Find values in 2D array corresponding to vector of indices obtained from another array

11 ビュー (過去 30 日間)
Hello!
I have a 3 x (variable number, in this case 5) array amp:
amp =
Columns 1 through 5
0.04 0.05 0.06 0.07 0.05
0.07 0.03 0.02 0.06 0.06
0.06 0.03 0.06 0.04 0.07
Indices idx of the maximum value for each column are as follows:
idx =
2 1 1 1 3
I have another 3 x 5 array frq
frq =
50 16 18 50 50
50 25 25 50 43
47 20 20 50 39
and would like to get a vector of the values in frq that correspond to the indices for the row shown in idx. That should be
50 16 18 50 39
frq(idx) or frq(:,idx) come up with a mess, and I have tried to use sub2ind and also come up with a mess. I'm sure this could be done in a loop easily, but would like to know how to do it the "MATLAB" way!
Thanks.
Doug Anderson

採用された回答

Voss
Voss 2022 年 4 月 16 日
編集済み: Voss 2022 年 4 月 16 日
amp = [0.04 0.05 0.06 0.07 0.05
0.07 0.03 0.02 0.06 0.06
0.06 0.03 0.06 0.04 0.07];
frq = [50 16 18 50 50
50 25 25 50 43
47 20 20 50 39];
[~,idx] = max(amp,[],1);
[m,n] = size(frq);
result = frq(sub2ind([m n],idx,1:n))
result = 1×5
50 16 18 50 39
  2 件のコメント
Douglas Anderson
Douglas Anderson 2022 年 4 月 16 日
Thank you! That clears up my sub2ind mistakes.
Voss
Voss 2022 年 4 月 16 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by