简体   繁体   中英

Matlab multiply matrix with matrix along third dimension

I have a matrix A of size mxnx 3 and I have have a 3x3 matrix K . Now what I want to do is something like this:

for row = 1:m
 for col = 1:n
  A(row,col,:) = K*[A(row,col,1);A(row,col,2);A(row,col,3)];
 end
end

I'd like to have an efficient solution without a loop, as loops are very slow, because mxn is usually the size of an image.

Anyone got an idea?

M = 1000;
N = 1000;
L = 3;
A = rand(M,N,L);
K = rand(L,L);
Q = reshape((K * reshape( A, [M*N, L] ).' ).', [M, N, L]);

Error check:

Z = zeros(M,N,L);
for mm = 1 : M
  for nn = 1 : N
    Z(mm,nn,:) = K * squeeze( A(mm,nn,:) );
  end
end
max( abs( Z(:) - Q(:) ) )

ans =

      0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM