且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将3D矩阵与2D矩阵相乘

更新时间:2022-11-10 10:43:15

您可以使用 CELLFUN 以便在所有单元中进行操作:

You can do this in one line using the functions NUM2CELL to break the matrix X into a cell array and CELLFUN to operate across the cells:

Z = cellfun(@(x) x*Y,num2cell(X,[1 2]),'UniformOutput',false);

结果Z是一个 1-by-C 单元格数组,其中每个单元格都包含一个 A-by-D 矩阵.如果希望Z成为 A-D-D-C 矩阵,则可以使用

The result Z is a 1-by-C cell array where each cell contains an A-by-D matrix. If you want Z to be an A-by-D-by-C matrix, you can use the CAT function:

Z = cat(3,Z{:});


注意::我的旧解决方案使用了 MAT2CELL 而不是 NUM2CELL ,不那么简洁:

NOTE: My old solution used MAT2CELL instead of NUM2CELL, which wasn't as succinct:

[A,B,C] = size(X);
Z = cellfun(@(x) x*Y,mat2cell(X,A,B,ones(1,C)),'UniformOutput',false);