Rewriting a value with a loop
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jos (10584)
le 19 Déc 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 commentaires
Jos (10584)
le 19 Déc 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!