How to assign variables.

조회 수: 5 (최근 30일)
srycandy
srycandy 2011년 6월 16일
Hi. I am a beginner in Matlab.. wanna seek for help from Matlab pro...i wrote codes: {i=1:5; j=5; x = [0.1 0.2 0.3 0.4 0.5]; for i = 1:5 for j = 5 X = x(i:j) end end}
and the outputs are:
X =
0.1000 0.2000 0.3000 0.4000 0.5000
X =
0.2000 0.3000 0.4000 0.5000
X =
0.3000 0.4000 0.5000
X =
0.4000 0.5000
X =
0.5000
How can I assign a variable X(1),X(2),...,X(5) for each X respectively i.e X(1)=[0.1 0.2 0.3 0.4 0.5], X(2)=[0.2 0.3 0.4 0.5] and so on.... Your cooperation is very much appreciated

채택된 답변

Laura Proctor
Laura Proctor 2011년 6월 16일
You can do this using cell arrays:
x = [0.1 0.2 0.3 0.4 0.5];
for i = 1:5
X{i,1} = x(i:end)
end
Then, you can access the contents in each cell using curly brackets:
X{1}
  댓글 수: 1
srycandy
srycandy 2011년 6월 16일
thanks for ur answer. I'll try it..

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 6월 16일
h = .1:.1:.5;
x = arrayfun(@(jj)h(jj:end),1:size(H,1),'un',0)
  댓글 수: 1
srycandy
srycandy 2011년 6월 16일
thanks

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by