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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by