MATLAB 中的向量到矩阵语法
有没有办法在 MATLAB 中组合 2 个向量,以便:
mat = zeros(length(C),length(S));
for j=1:length(C)
mat(j,:)=C(j)*S;
end
使用类似于以下的正常 MATLAB 语法:
mat = C * S(1:length(S))
这会给出“内部矩阵维度必须一致错误”,因为它试图执行正常的矩阵运算。 这不是标准的线性代数运算,所以我不确定如何在 MATLAB 中正确表达它,但似乎不需要循环就可以实现,而循环在 MATLAB 中太慢了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从你的描述来看,这听起来像是一个简单的矩阵运算。 您只需确保 C 和 S 的尺寸正确即可。 C 应该是列向量 (length(C)-by-1),S 应该是行向量 (1-by-<强>长度(S))。 假设它们的尺寸正确,只需执行以下操作:
如果您不确定它们的尺寸,这应该可行:
编辑:实际上,我对括号有点疯狂。 其中一些是不必要的,因为不存在操作顺序问题。 这是一个更清晰的版本:
说明:
矩阵乘法运算符将产生内积(结果标量值)或外积(生成矩阵),具体取决于它所应用到的向量的维度。
由于使用了 ,上面的最后一个方程产生了一个外积冒号运算符 来重塑向量参数的维度。 语法 C(:) 将 C 的内容重塑为单个列向量。 语法 S(:)' 将 S 的内容重塑为列向量,然后将其转置为行向量。 相乘时,会产生一个大小为(length(C)-by-length(S))的矩阵。
注意:冒号运算符的这种用法适用于任何维度的向量和矩阵,允许您将它们的内容重新整形为单个列向量(这使得某些操作更容易,如 这个其他问题)。
From your description, it sounds like a simple matrix operation. You just have to make sure you have the right dimensions for C and S. C should be a column vector (length(C)-by-1) and S should be a row vector (1-by-length(S)). Assuming they are the right dimensions, just do the following:
If you're not sure of their dimensions, this should work:
EDIT: Actually, I went a little crazy with the parentheses. Some of them are unnecessary, since there are no order-of-operation concerns. Here's a cleaner version:
EXPLANATION:
The matrix multiplication operator in MATLAB will produce either an inner product (resulting in a scalar value) or an outer product (resulting in a matrix) depending on the dimensions of the vectors it is applied to.
The last equation above produces an outer product because of the use of the colon operator to reshape the dimensions of the vector arguments. The syntax C(:) reshapes the contents of C into a single column vector. The syntax S(:)' reshapes the contents of S into a column vector, then transposes it into a row vector. When multiplied, this results in a matrix of size (length(C)-by-length(S)).
Note: This use of the colon operator is applicable to vectors and matrices of any dimension, allowing you to reshape their contents into a single column vector (which makes some operations easier, as shown by this other SO question).
尝试在 MATLAB 中执行此操作:
如下所示:
Try executing this in MATLAB:
As In:
您的意思是以下内容吗?
如果是这样,那就只是矩阵乘法:
如果你不知道
C
和S
是行向量还是列向量,你可以使用一个技巧将它们变成列向量,然后在将它们相乘之前转置S
:Do you mean the following?
If so, it's simply matrix multiplication:
If you don't know whether
C
andS
are row vectors or column vectors, you can use a trick to turn them into column vectors, then transposeS
before multiplying them:我不完全清楚你在做什么 - 看起来你的结果矩阵将由
length(C)
行组成,其中第i
行是向量S
按C
的第i
条目缩放(因为向量的下标给出了标量)。 在这种情况下,您可以执行类似在列之间平铺
C
以及在行之间平铺S
的操作。I'm not entirely clear on what you're doing - it looks like your resulting matrix will consist of
length(C)
rows, where thei
th row is the vectorS
scaled by thei
th entry ofC
(since subscripting a vector gives a scalar). In this case, you can do something likewhere you tile
C
across columns, andS
down rows.试试这个:
C = 1:3
S = 1:5
mat1 = C'*S
mat2 = bsxfun(@times, C',S)
(特别是当您需要的函数不是更简单的 MATLAB 表示法时,这很好)
--Loren
Try this:
C = 1:3
S = 1:5
mat1 = C'*S
mat2 = bsxfun(@times, C',S)
(esp. good when the function you need isn't simpler MATLAB notation)
--Loren
尝试使用网格网格:
编辑:没关系,矩阵乘法也可以。 你只需要一个列向量C和一个行向量S。然后做C * S。
Try using meshgrid:
edit: nevermind, matrix multiplication will do too. You just need one column vector C and one row vector S. Then do C * S.