simplify the matlab coding
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
David Sanchez
il 9 Ott 2013
Don't yo have a X3? Who is Xa? Are you sure that what your code here is what you want?
Jan
il 9 Ott 2013
@Onion Lim: Please notice, that nearly all questions in this forum concern "matlab" and "matlab code". Therefore these are not useful tags.
Risposte (2)
ES
il 9 Ott 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 Commenti
Jan
il 9 Ott 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)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!