Assigning multiple values of a 3d matrix to another matrix

2 visualizaciones (últimos 30 días)
sundar
sundar el 5 de Jun. de 2017
Respondida: sundar el 5 de Jun. de 2017
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.

Respuesta aceptada

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

Más respuestas (1)

sundar
sundar el 5 de Jun. de 2017
Great! Thanks for the solution :)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by