how can i add more than two elements to an array ?

if i have
x=[1,2,3,4]
syms t
i=1 ;
for i=1:3
switch x(i)
case 1
x1=cos(t)
case 2
x2=sin(t)
case 3
x3=sin(2t)
end
i=i+1
end
i want to have array x that contains y=[x1,x2,x3]
so x=[cos(t),sin(t),sin(2t),cos(7t)]
i want a method to this operation because i want to use it in a loop or switch ?
how can i do this in MATLAB ?

 채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 11일

0 개 추천

syms t
x1=cos(t)
x2=sin(t)
x3=sin(2*t)
x4=cos(7*t)
x = [x1, x2, x3, x4]

댓글 수: 5

Eng Abeer
Eng Abeer 2015년 12월 11일
thanks but i want a method to do this if i have a loop i can't use x = [x1, x2, x3, x4] is there any method do this ?
x=[1,2,3,4]
syms t
for i=1:length(x)
switch x(i)
case 1
v = cos(t);
case 2
v = sin(t);
case 3
v = sin(2*t);
case 4
v = cos(7*t);
end
y(i) = v;
end
Eng Abeer
Eng Abeer 2015년 12월 11일
편집: Walter Roberson 2015년 12월 11일
Thanks ... it is very useful way
x=[1,2,3,4];
y=[];
syms t
for i=1:length(x)
switch x(i)
case 1
v = cos(t);
case 2
v = sin(t);
case 3
v = sin(2*t);
case 4
v = cos(7*t);
end
y=cat(2,y,x) ;
end
can i use this y=cat(2,y,x) or it will be wrong ??
y = cat(2,y,x) should work. Most people would instead write y = [y,x] or y = horzcat(y,x) . But better yet is to use indexing and an initialized matrix
y = sym(zeros(1,length(x));
...
y(i) = v;
Eng Abeer
Eng Abeer 2015년 12월 11일
Thanks...

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

추가 답변 (0개)

태그

질문:

2015년 12월 11일

댓글:

2015년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by