84.6k views
0 votes
What is the Matlab command to create a vector of the even whole numbers between 29 and 73?

User Ivoszz
by
5.4k points

1 Answer

7 votes

Answer:

x = 29:73;

x_even = x(2:2:end);

Step-by-step explanation:

In order to create a vector in Matlab you can use colon notation:

x = j:k

where j is 29 and k is 73 in your case:

x = 29:73

Then you can extract the even numbers by extracting the numbers with even index (2,4,6,etc.) of your vector:

x_even = x(2:2:end);

In the line of code above, we define x_even as all the elements of x with even index from index 2 till the end index of your vector, and an increment of 2 in the index: 2,4,6,etc.

If you want the odd numbers, just use the odd indices of your vector:

x_odd = x(1:2:end);

where x_odd contains all the elements of x with odd index from index 1 till the end index, and an increment of 2: 1,3,5,etc.

User Chenlian
by
5.1k points