Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
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 :-)

Respuesta aceptada

KSSV
KSSV el 3 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by