simplify the matlab coding

9 views (last 30 days)
ONION LIM
ONION LIM on 9 Oct 2013
Commented: Jan on 9 Oct 2013
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 Comments
David Sanchez
David Sanchez on 9 Oct 2013
Don't yo have a X3? Who is Xa? Are you sure that what your code here is what you want?
Jan
Jan on 9 Oct 2013
@Onion Lim: Please notice, that nearly all questions in this forum concern "matlab" and "matlab code". Therefore these are not useful tags.

Sign in to comment.

Answers (2)

ES
ES on 9 Oct 2013
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 Comments
Jan
Jan on 9 Oct 2013
@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 on 9 Oct 2013
i see, thanks

Sign in to comment.


Walter Roberson
Walter Roberson on 9 Oct 2013

Tags

Community Treasure Hunt

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

Start Hunting!