MATLAB Answers

Extracting indices from 3D matrix for another 3D matrix

2 views (last 30 days)
Bismark Singh
Bismark Singh on 29 Mar 2020
Answered: Matt J on 6 Apr 2020
I have two 3D matrices A and B, of the same size and another 3d matrix I of the same size containing some indices corresponding to the first dimension of A. I want to do something like A(I)/B(I).
Specifically, A(I(x,y,z),y,z) divide it by B(I(x,y,z),y,z) for all of x,y,z. How do I do it quickly maybe using repmat or sub2ind or simply logical indexing?
If it is not clear I can post an example.

  1 Comment

Jyotsna Talluri
Jyotsna Talluri on 6 Apr 2020
You can use logical indexing in order to access the values from the matrices,but there might be a possibility that the value of I(x,y,z) is greater than the number of rows of matrices A and B

Sign in to comment.

Answers (1)

Matt J
Matt J on 6 Apr 2020
[m,n,p]=size(A);
[~,J,K]=ndgrid(1:m,1:n,1:p);
idx=sub2ind([m,n,p],I,J,K);
A(idx)./B(idx)

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags