Rewriting a value with a loop
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!