Mapping arrays of different dimensions

2 views (last 30 days)
azr124
azr124 on 9 Jul 2018
Commented: azr124 on 9 Jul 2018
Hi there, I have the following 2x2x2 dimensional array A(:,:,1) = [1 2; 3 4] and A(:,:,2) = [5 6; 7 8] and the following 3 vectors of size 1x5 that store indices related to A: i = [1 1 1 2 2], j = [2 2 2 2 2] and k = [1 1 2 2 1].
I would like to obtain a vector B of size 1x5 that stores the entries of A as follows:
B(1,1) = A(i(1), j(1), k(1)), so B(1,1) = A(1,2,1) = 2
B(1,2) = A(i(2), j(2), k(2)), so B(1,2) = A(1,2,1) = 2
B(1,3) = A(i(3), j(3), k(3)), so B(1,3) = A(1,2,2) = 6
And so on… such that in the end B = [2 2 6 8 4],
without using loops.
I hope you can help me with this. Many thanks!

Accepted Answer

Jan
Jan on 9 Jul 2018
A(:,:,1) = [1 2; 3 4]
A(:,:,2) = [5 6; 7 8]
i = [1 1 1 2 2];
j = [2 2 2 2 2];
k = [1 1 2 2 1];
index = sub2ind(size(A), i, j, k);
B = A(index)

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!