Rewriting a value with a loop

조회 수: 7 (최근 30일)
Dennis
Dennis 2013년 12월 19일
댓글: Jos (10584) 2013년 12월 19일
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.

채택된 답변

Jos (10584)
Jos (10584) 2013년 12월 19일
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
Dennis
Dennis 2013년 12월 19일
That works great! Thanks a lot for your help.
Jos (10584)
Jos (10584) 2013년 12월 19일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by