How to indexing directly multiple row?
조회 수: 4 (최근 30일)
이전 댓글 표시
Dear all, I wonder how we can do direct indexing in compact form, for the following case? In this problem, i want to extract two rows for different columns. To achieve the objective, the following code was realize. But, the code is lengthy, and I wonder if we can simplify this.
Thanks in advance.
ShufleNperc=5;
randm_row_NUm=[randperm(ShufleNperc);randperm(ShufleNperc)]; % Row idx
randm_no=randi(50,ShufleNperc,ShufleNperc); % value to be extracted base on the row
vvvv=zeros(size(randm_row_NUm,1),ShufleNperc); % Prelocate memory
for x_v=1:ShufleNperc
vvvv(:,x_v)=randm_no((randm_row_NUm(:,x_v))',x_v);
end
댓글 수: 0
채택된 답변
KL
2017년 12월 4일
편집: KL
2017년 12월 4일
use linear indices,
With your example,
colInd = repmat(1:size(randm_no,2),2,1);
linind = sub2ind(size(randm_no),randm_row_NUm(:),colInd(:));
vvvv_simple = randm_no(linind);
you can reshape it if you want,
vvvv_simple = reshape(vvvv_simple,size(vvvv))
the result is the same for both methods,
>> vvvv
vvvv =
48 48 33 38 3
8 22 43 33 5
>> vvvv_simple
vvvv_simple =
48 48 33 38 3
8 22 43 33 5
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!