フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by