Can anyone show me how I can avoid following for loops
Ältere Kommentare anzeigen
*Hello everyone,
Can anyone show me how I can avoid following for loops.
Thanks!*
mth=0;
nth=0;
for i=1:1000
ML = [1,2;3,4];
for ix=1:size(ML,1)
for iy=1:size(ML,2)
M(mth+ix,nth+iy)=ML(ix,iy);
end
end
mth = mth+size(ML,1);
nth = nth+size(ML,2);
end
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 8 Okt. 2012
for iy=1:size(ML,2)
M(mth+ix,nth+iy)=ML(ix,iy);
end
can be written as
MLc = size(ML,2);
M(mth+ix, nth:nth+MLc-1) = ML(ix, 1:MLc);
3 Kommentare
Daniel Shub
am 8 Okt. 2012
Bearbeitet: Daniel Shub
am 8 Okt. 2012
Are you sure? If nth is 0 and MLc is 2, nth:nth+MLc-1 gives [0, 1] which causes indexing problems. I think it is nth+(1:MLc) on the RHS.
Walter Roberson
am 8 Okt. 2012
Bearbeitet: Walter Roberson
am 8 Okt. 2012
Yes, that would make sense on the LHS.
Daniel Shub
am 8 Okt. 2012
Yes, LHS not RHS...
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!