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?
1 次查看(过去 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 :-)
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!