how do i use the indices from [M,I]=max(A,[],3) to find the corresponding values in another 3D array, B, of the same size as A?

2 次查看(过去 30 天)
I have two 3d arrays, A and B, of size n,m,k.
I use [M,I]=max(A,[],3) to get the indices with the highest values along the 3rd axis. M and I are therefore matrices of size n,m.
I want to get the values of B at the indices given in I. The result should be another n,m matrix.
What is the best way to get these values, without using for loops over n and m?
Thank you for you time :-)

采纳的回答

KSSV
KSSV 2016-11-3
clc; clear all ;
n = 10 ; m = 7 ; k = 10 ;
A = rand(n,m,k) ;
B = rand(n,m,k) ;
[M,I]=max(A,[],3) ;
% using loop
iwant1 = zeros(size(M)) ;
for i = 1:n
for j = 1:m
iwant1(i,j) = B(i,j,I(i,j)) ;
end
end
% without using loop
B2D = reshape(B,n*m,k) ;
B2D = B2D(:,I(:)) ;
B2D = diag(B2D) ;
iwant2 = reshape(B2D,n,m) ;
You can compare iwant1 and iwant2.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by