Rewriting a value with a loop
6 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jos (10584)
on 19 Dec 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 Comments
Jos (10584)
on 19 Dec 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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!