Assigning multiple values of a 3d matrix to another matrix

조회 수: 2 (최근 30일)
sundar
sundar 2017년 6월 5일
답변: sundar 2017년 6월 5일
I have the following problem. Say I have a 3D image stored in variable I and three 2D matrices that contains x,y,z points of interest as follows.
I = rand(10,10,10);
x = [3 4];
y = [5 6];
z = [1 2];
Now, I want to make a new variable Inew, which contains pixel intensities corresponding to these x,y,z positions.
Inew(1,1) = I(x(1,1),y(1,1),z(1,1));
Inew(1,2) = I(x(1,2),y(1,2),z(1,2));
How to perform this action without going through a 'for' loop that goes through point by point? In my actual problem, all these matrices are huge. I is 900 by 700 by 1000 and x,y,z are each 900 by 3600. Going through point by point is very time consuming.
In the given example, if I just say, 'Inew = I(x,y,z);' it will obviously give me an output that is of size 2 by 2 by 2, which is not what I want.
Any help will be great.

채택된 답변

Guillaume
Guillaume 2017년 6월 5일
Use sub2ind to convert your x, y, z into linear indices:
Inew = I(sub2ind(size(I), x, y, z));

추가 답변 (1개)

sundar
sundar 2017년 6월 5일
Great! Thanks for the solution :)

카테고리

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