Rewriting a value with a loop
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dennis
am 19 Dez. 2013
Kommentiert: Jos (10584)
am 19 Dez. 2013
Hello,
I would like to rewrite a value in Matlab using a loop. The value vl (20x601) should be rewritten so only the rows written in hright (15 rows instead of the original 20) are used.
I tried the following, but this constantly rewrites vlnew making all rows the same.
for i = hright for j = 1:length(hright) vlnew(j,:)=vl(i,:); end end
Does anyone have a suggestion how to do this? Thank you in advance.
0 Kommentare
Akzeptierte Antwort
Jos (10584)
am 19 Dez. 2013
VL = bsxfun(@plus,(1:9).',[10 20 30]) % some example data
RowsToSelect = [1 3 4 7]
VLnew = VL(RowsToSelect,:)
% or with a for-loop, which is much slower and is going against
% the reason why you want to use matlab in the first place ...
for k = 1:numel(RowsToSelect)
VLnew2(k,:) = VL(RowsToSelect(k),:) ;
end
2 Kommentare
Jos (10584)
am 19 Dez. 2013
You're welcome. Not that you can multi-select and switch using the indexing trick quite easily. This really shows the power of matlab!
A = [11 12 ; 21 22 ; 31 32 ; 41 42]
idx = [4 1 2 2 2] % row indices
B = A(idx,:) % selection
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!