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)

请先登录,再进行评论。


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