Assigning multiple values of a 3d matrix to another matrix

2 次查看(过去 30 天)
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