Extract indices from other matrix corresponding to the minimum

조회 수: 1 (최근 30일)
KostasK
KostasK 2022년 2월 7일
편집: Matt J 2022년 2월 7일
I currently have a matrix in which I want to determine the column which corresponds to the minimum of each row, which I can do the following way:
Mat = rand(30, 2) ;
[~, min_idx] = min(Mat, [], 2, 'linear') ;
[~, col] = ind2sub(size(Mat), min_idx) ;
Following that, I would like to use col, to extract the corresponding 3rd dimension in another matrix X:
X = rand(30, 3, 2) ;
Xmin = X(:,:,col) ;
For Xmin I would expect to get a 2D matrix, where each row corresponds to the index col in the 3rd dimention extracted from X, however that is not the case for some reason. Do you know how I could accomplish the above?
Thanks for your help in advance.

채택된 답변

Matt J
Matt J 2022년 2월 7일
편집: Matt J 2022년 2월 7일
Mat = rand(30,2);
X = rand(30, 3, 2) ;
[~, min_idx] = min(Mat, [], 2, 'linear') ;
Xp=reshape( permute(X,[1,3,2]),[],3);
Xmin=Xp(min_idx,:)
Xmin = 30×3
0.0712 0.3075 0.8858 0.6488 0.0418 0.7136 0.9145 0.4471 0.9905 0.1935 0.1567 0.1708 0.1016 0.3393 0.3916 0.8534 0.8261 0.4123 0.2960 0.7503 0.8993 0.1845 0.9356 0.2080 0.7731 0.7444 0.5096 0.5429 0.7494 0.9430

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by