simplify the matlab coding

9 visualizaciones (últimos 30 días)
ONION LIM
ONION LIM el 9 de Oct. de 2013
Comentada: Jan el 9 de Oct. de 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 comentarios
David Sanchez
David Sanchez el 9 de Oct. de 2013
Don't yo have a X3? Who is Xa? Are you sure that what your code here is what you want?
Jan
Jan el 9 de Oct. de 2013
@Onion Lim: Please notice, that nearly all questions in this forum concern "matlab" and "matlab code". Therefore these are not useful tags.

Iniciar sesión para comentar.

Respuestas (2)

ES
ES el 9 de Oct. de 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 comentarios
Jan
Jan el 9 de Oct. de 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 el 9 de Oct. de 2013
i see, thanks

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 9 de Oct. de 2013

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by