内部矩阵尺寸必须一致 [英] Inner matrix dimensions must agree

查看:83
本文介绍了内部矩阵尺寸必须一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵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屋!

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