内部矩阵尺寸必须一致 [英] Inner matrix dimensions must agree
问题描述
我有一个矩阵A
和一个矢量x
:
I have a matrix A
and a vector x
:
A
是50x30的矩阵
x
是1x30向量
我想用x
乘以A
,但是每当尝试使用z = A * x
时,都会出现错误Inner matrix dimensions must agree.
,但是可以肯定的是,在相同列数的情况下,矩阵维数确实一致吗?
I want to multiply A
by x
, yet whenever I try z = A * x
I get the error Inner matrix dimensions must agree.
Yet surely with the same amount of columns the matrix dimensions do agree?
我对为什么这样做感到困惑:
I'm confused as to why this works:
A = rand(2,2);
x = [1;2];
A * x
但是这不起作用:
A = rand(2,2);
x = 1:2;
A * x
推荐答案
转置第二个参数:
z = A * x.'
错误提示-内部矩阵尺寸必须一致-您有
A = [50x30]
和x = [1x30]
,内部尺寸为 30 和 1 .
As the error suggest - the inner matrix dimensions must agree - you have
A = [50x30]
and x = [1x30]
, the inner dimensions are 30 and 1.
通过转置得到A = [50x30]
和x = [30x1]
,内部尺寸分别为 30 和 30 ,
By tranposing you get A = [50x30]
and x = [30x1]
, the inner dimensions are then 30 and 30, agreeing.
这篇关于内部矩阵尺寸必须一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!