simplify the matlab coding

조회 수: 9 (최근 30일)
ONION LIM
ONION LIM 2013년 10월 9일
댓글: Jan 2013년 10월 9일
guys, i got a code like this
X0=[938.857; 894.100; 956.706; 878.926; 1021.071; 911.951; 959.432; 893.034; 954.470]
X2=X0(1);
Y2=X0(2);
Y3=X0(3);
X4=X0(4);
Y4=X0(5);
X5=X0(6);
Y5=X0(7);
X6=X0(8);
Y6=X0(9)
how can i simplify it?
and also this
for number = 1:n;
iteration = number;
if iteration ==1;
fprintf('Iteration = %2.0f\n',iteration)
X0=X0;
X2=X0(1);
Y2=X0(2);
Y3=X0(3);
X4=X0(4);
Y4=X0(5);
X5=X0(6);
Y5=X0(7);
X6=X0(8);
Y6=X0(9);
else
fprintf('Iteration = %2.0f\n',iteration)
X0=Xa;
X2=X0(1);
Y2=X0(2);
Y3=X0(3);
X4=X0(4);
Y4=X0(5);
X5=X0(6);
Y5=X0(7);
X6=X0(8);
Y6=X0(9);
end
any idea how to simplify it?
  댓글 수: 2
David Sanchez
David Sanchez 2013년 10월 9일
Don't yo have a X3? Who is Xa? Are you sure that what your code here is what you want?
Jan
Jan 2013년 10월 9일
@Onion Lim: Please notice, that nearly all questions in this forum concern "matlab" and "matlab code". Therefore these are not useful tags.

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

답변 (2개)

ES
ES 2013년 10월 9일
I would suggest you to use arrays instead of variables like X1, X2, Y1, Y2 etc. That is, in place of X1, X2, Y1, Y2 etc use X(1), X(2). You can iterate through input and assign to X(i).
However, if you need to use X1, X2 etc for some reason, try eval(['X',num2str(i),'=X0'])
  댓글 수: 3
Jan
Jan 2013년 10월 9일
@Onion Lim: Please read this and Walter's answer again until you understand it. A pile of variables called X1, X2, X3, ... is a really bad idea, because the index is hidden inside the name. Better use and index as index: X(1), X(2), X(3), ... In your case something like this means:
X = X0(1:2:end)
Y = X0(2:2:end)
ONION LIM
ONION LIM 2013년 10월 9일
i see, thanks

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


Walter Roberson
Walter Roberson 2013년 10월 9일

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by