Vectorizing inside a loop
Mostra commenti meno recenti
I am trying to implement something like this
for i=1:n
matrix(i:n:end,4:6)=y(i,:)-matrix(i,1:3);
end
I was under the impression that vectorizing would allow me to eliminate the need for another loop but I receive 'Subscripted assignment dimension mismatch' when I attempt to evaluate this. I believe it is because it is not assigning it on a per row basis as I had hoped.
Is it at all possible to accomplish this without adding more loops?
3 Commenti
ChristianW
il 20 Mar 2013
Modificato: ChristianW
il 20 Mar 2013
What is size(y) and size(matrix)? Which value has n? What are you trying with i:n:end?
The right part of your equation is a 1x3 vector and the left part is a mx3 matrix, thats why dimensions mismatch.
Avery
il 20 Mar 2013
ChristianW
il 20 Mar 2013
Show your working double loop.
Risposte (1)
Babak
il 20 Mar 2013
The problem with
matrix(i:n:end,4:6)=y(i,:)-matrix(i,1:3);
is that the first index i:n:end is a vector and does not correspond to 1 element. On the other hand, on the righe side, the index i is a sinle element which chooses the row with index i.
You need to change i:n:end to a proper single index accordign to what you want to write to. If you give a numerical example, maybe I can figure what you really want to do.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!