how to find index from matrix in another matrix?

조회 수: 2 (최근 30일)
Lilya
Lilya 2022년 7월 26일
댓글: Fangjun Jiang 2022년 7월 27일
Hi all,
I have two different 3d matrices (A=72*46*2192) and (B=72*46*2192), in which I want to find the indices equal to 4 and 5 from A in B.
The result should be a 3d matrix as well, not linear indices.
Any help would be appreciated.
  댓글 수: 2
David Hill
David Hill 2022년 7월 26일
I do not understand your question. A simple example might help.
Lilya
Lilya 2022년 7월 27일
Thanks for your responce David.
A = rand(2,3,4);
B=rand(2,3,4);
Matrix A should have elements = 4 and 5.
I want to use the index of those 4 and 5 in A to find the corresponding elements in B.

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

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 7월 26일
편집: Fangjun Jiang 2022년 7월 27일
%C is the logical index matrix.
A=zeros(2,3,4);
A(:,:,4)=4;
A(:,3,4)=5;
C=or(A==4,A==5)
C = 2×3×4 logical array
C(:,:,1) = 0 0 0 0 0 0 C(:,:,2) = 0 0 0 0 0 0 C(:,:,3) = 0 0 0 0 0 0 C(:,:,4) = 1 1 1 1 1 1
%To use it to select corresponding elements in B
B=rand(2,3,4);
B_select=B(C)
B_select = 6×1
0.2953 0.0888 0.5716 0.4577 0.5288 0.3675
LinearIndex=find(C);
[SubX,SubY,SubZ]=ind2sub(size(C),LinearIndex)
SubX = 6×1
1 2 1 2 1 2
SubY = 6×1
1 1 2 2 3 3
SubZ = 6×1
4 4 4 4 4 4
  댓글 수: 2
Lilya
Lilya 2022년 7월 27일
Thank you so much.
but how can i return the index to matrix B?
not a linear result
Fangjun Jiang
Fangjun Jiang 2022년 7월 27일
See the updated answer to understand
  1. Logical index
  2. Linear index
  3. Subscript index

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by