236 views (last 30 days)

I have to form a row vector x=[5 6 16 17 27 28 38 39....] till the total number of elements become 'n' where 'n' is entered by the user. Say, I enter n=4 so my x=[5 6 16 17]

basically what I want is that if I have two numbers say, X and Y and I wish to give some increment to these numbers to form a row matrix containing 'n' elements. Say, increment is 3 and n is 6 A=[X Y X+3 Y+3 X+6 Y+6]

Michael Haderlein
on 9 Mar 2015

First, get all the X+... and Y+... separately and get them into the correct order then (Activate the commented % to stop the output as it's only for educational reasons here):

>> inc=11;X=5;Y=6;

>> N=3;

>> Z=[X+(0:N-1)*inc;Y+(0:N-1)*inc] %; %first row: X+..., second row: Y+...

Z =

5 16 27

6 17 28

>> Z(:)'

ans =

5 6 16 17 27 28

Stephen Cobeldick
on 9 Mar 2015

To get the output to be length N, as the original question requested, try this:

>> inc = 11;

>> X = 5;

>> Y = 6;

>> N = 3;

>> Z = inc*(0:N/2);

>> Z = [X+Z; Y+Z];

>> Z = Z(1:N);

Chandrasekhar
on 9 Mar 2015

x = [5 6 16 17 27 28 38 39....]

n = input('enter the total number of elements');

x = x[1:n];

disp(x);

Jan
on 9 Mar 2015

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/182184-form-a-row-vector#comment_270738

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/182184-form-a-row-vector#comment_270738

Sign in to comment.