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
Michael Haderlein 2015년 3월 9일

1 개 추천

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

댓글 수: 2

Ace_ventura
Ace_ventura 2015년 3월 9일
Thanks Michael.
Stephen23
Stephen23 2015년 3월 9일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chandrasekhar
Chandrasekhar 2015년 3월 9일

0 개 추천

x = [5 6 16 17 27 28 38 39....]
n = input('enter the total number of elements');
x = x[1:n];
disp(x);

댓글 수: 4

Ace_ventura
Ace_ventura 2015년 3월 9일
I don't think it will work.Can we define x the way you have defined?
Ace_ventura
Ace_ventura 2015년 3월 9일
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]
Jan
Jan 2015년 3월 9일
@Jeff: What have you tried so far and which problems occur? It is much easier to answer, if you show us what you have done so far.
x=5;y=6;
a=(x:2:10);
A=a';
b=(y:2:11);
B=b';
c=horzcat(A,B)
c =
5 6
7 8
9 10
>> d=c(:)'
d =
5 7 9 6 8 10
but now I have got it

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2015년 3월 9일

댓글:

2015년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by