Effacer les filtres
Effacer les filtres

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 vues (au cours des 30 derniers jours)
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 :-)

Réponse acceptée

KSSV
KSSV le 3 Nov 2016
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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by