Assigning multiple values of a 3d matrix to another matrix

2 views (last 30 days)
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.

Accepted Answer

Guillaume
Guillaume on 5 Jun 2017
Use sub2ind to convert your x, y, z into linear indices:
Inew = I(sub2ind(size(I), x, y, z));

More Answers (1)

sundar
sundar on 5 Jun 2017
Great! Thanks for the solution :)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!