Stuart’s MATLAB Videos

Watch and Learn

Video tutorial: 3d indexing

The MATLAB user has this:

clear
clc
M(:,:,1) = [1,2;3,4];
M(:,:,2) = [21,22;23,24];
J = [1,2; 2,1];
They want this:
ans =
[1 22
23 4]
They tried this:
D = M(:,:,J) % This doesn't work,
This video discusses this solution:
[nR, nC] = size(J);
D = zeros(nR,nC);
for r = 1:nR
     for c = 1:nC
          D(r,c) = M(r,c,J(r,c));
    end
end     
 
|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.