How to retrieve multiple values from a 3D array

1 view (last 30 days)
Dear All,
I have a 3D matrix
I_dd (212x210x245)
and a 2D matrix
occupied_box_indices(4x3) = [156 151 34
156 151 33
156 150 33
177 74 5]
I want to create a vector using these matrcies as below:
I_adress_boxes(1) = I_dd(occupied_box_indices(1,1),occupied_box_indices(1,2),occupied_box_indices(1,3));
I_adress_boxes(2) = I_dd(occupied_box_indices(2,1),occupied_box_indices(2,2),occupied_box_indices(2,3));
I_adress_boxes(3) = I_dd(occupied_box_indices(3,1),occupied_box_indices(3,2),occupied_box_indices(3,3));
I_adress_boxes(4) = I_dd(occupied_box_indices(4,1),occupied_box_indices(4,2),occupied_box_indices(4,3));
With a for loop it can be done very easliy. However, I wonder if is it possible to cerate the I_adress_boxes without for loop and which way do you think is the best in terms of computation time?

Accepted Answer

dpb
dpb on 6 May 2021
Ignoring the excessively verbose variable names that keeps code from being visible without scrolling or breaking line and thereby harder to read to get matching parens right...
I_adress_boxes=I_dd(ind2sub(size(I_dd),occupied_box_indices(:,1), ...
occupied_box_indices(:,2), ...
occupied_box_indices(:,3)));

More Answers (0)

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!