Here is an example of code that executes what I'd like to do, but is very slow for large arrays and can likely be written much more simply and faster. I have an array A that I take the max over the rows, where A is a 3d array. I'd then like to use the indices from the max operator to index into another array that is the same size as B and collect those elements in an array where the size of the final array is [1,size(A,2),size(A,3)]. Any help would be greatly appreciated. Thank you!
A = rand(3,2,10); [~,Aind] = max(A,[],1); B = rand(3,2,10); tmp3 = zeros(1,size(A,2),size(A,3)); for j = 1:size(A,2) tmp1 = squeeze(Aind(:,j,:))+ 3*((0:1:10-1)'); tmp2 = squeeze(B(:,j,:)); tmp3(1,j,:) = tmp2(tmp1); end
Best Answer