필터 지우기
필터 지우기

Index one multidimensional array using another

조회 수: 3 (최근 30일)
Rob Hurt
Rob Hurt 2022년 10월 31일
댓글: Rob Hurt 2022년 10월 31일
Hello,
Sorry for the basic question, but I couldn't find an answer on this forum.
Let's say I have two 4D arrays:
A = rand(3,5,4,2);
B = rand(3,5,4,2);
I want to get the max values in A along the third dimension and then pull the corresponding values out of B. What I tried to do was:
[maxs, maxs_ix] = max(A,[],3);
B(maxs_ix);
But this didn't give me the right answer. As a check, I would expect that the following two lines should return the exact same array, but they don't:
maxs;
A(maxs_ix);
Can someone explain what I'm missing, and how to get the values from B that are in the same positions as the maxs of A?

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 31일
The indices you get back from max are relative to the dimension searched over, except when you use the relatively new 'linear' or 'all' options.
Generally speaking if you get back an index relative to a dimension you typically end up needing to use sub2ind() or doing index arithmetic.
  댓글 수: 1
Rob Hurt
Rob Hurt 2022년 10월 31일
Thank you, I think the 'linear' argument is the answer to my question. Out of curiousity, how would I get the correct answer by using sub2ind()?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by