将 3D 矩阵与 2D 矩阵相乘 [英] Multiply a 3D matrix with a 2D matrix

查看:41
本文介绍了将 3D 矩阵与 2D 矩阵相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 AxBxC 矩阵 X 和一个 BxD 矩阵 Y.

Suppose I have an AxBxC matrix X and a BxD matrix Y.

是否有一种非循环方法可以将每个 C AxB 矩阵与 Y 相乘?

Is there a non-loop method by which I can multiply each of the C AxB matrices with Y?

推荐答案

您可以在一行中使用函数 NUM2CELL 将矩阵 X 分解为元胞数组和 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-by-D-by-C 矩阵,您可以使用 CAT 功能:

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);

这篇关于将 3D 矩阵与 2D 矩阵相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆