MATLAB: Multiply pages of 3D array with each other

3d arraymultiplication

Hi,
I have a 3D array (512x10xN) and would like to multiply all pages with each other, i.e.:
512x10x1 .* 512x10x2
512x10x1 .* 512x10x3
512x10x1 .* 512x10x4
512x10xN .* 512x10xN-1
N is up to 16. How can I perform this operation? I am not interested multiplying the same pages with each other, i.e.:
512x10x1 .* 512x10x1
512x10x2 .* 512x10x2
512x10x2 .* 512x10x3
512x10x3 .* 512x10x2
Thanks!

Best Answer

A = rand(512,10,16);
[m,n,N]=size(A);
pairs=nchoosek(1:N,2);
B=zeros(m,n,size(pairs,1));
for k=1:size(B,3)
B(:,:,k) = A(:,:,pairs(k,1)).*A(:,:,pairs(k,2));
end
Related Question