[英]Matlab 3D matrix transform 3rd dimension by 2D matrix vectorization
I have 3D matrix A
of size N, M, 3
and 2D transformation T
of size 3, 3
that I apply to the 3rd dimension of matrix A
. 我有3D矩阵
A
的大小N, M, 3
和2D变换T
大小的3, 3
,我适用于矩阵的第三维A
。 In case it's relevant, this 3D matrix is an RGB image. 如果相关,此3D矩阵是RGB图像。
My current solution looks like this: 我当前的解决方案如下所示:
for i = 1:N
for j = 1:M
A(i, j, :) = T * [A(i, j, r); A(i, j, g); A(i, j, b)];
end
end
But I'm curious if there is a way to vectorize it? 但是我很好奇是否有矢量化的方法吗?
You can try this. 你可以试试看 First switch the dimensions so the channel will be the first:
首先切换尺寸,使通道成为第一个:
B = permute(A,[3,1,2])
Then reshape B to be a matrix of size 3xN*M: 然后将B整形为大小为3xN * M的矩阵:
C = reshape(B,3,[])
And now compute a matrix multiplication: 现在计算一个矩阵乘法:
D = T*C;
You then need to reshape and permute back: 然后,您需要重塑和重新排列:
E = permute(reshape(D,3,M,N),[2,3,1])
I didn't test it so there may be something missing, but generally this should work (and fast!) 我没有对其进行测试,因此可能缺少一些东西,但是通常这应该可以工作(而且速度很快!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.