简体   繁体   English

执行'@minus'时出现bsxfun错误

[英]bsxfun error when perform '@minus'

I have two matrices to be subtracted. 我要减去两个矩阵。 Let's see the code below. 让我们看看下面的代码。

A=rand(5472,1);
B=rand(1,3);

C= bsxfun(@minus, A, B(:))

I get the error saying that 我得到的错误是

Non-singleton dimensions of the two input arrays must match each other 两个输入数组的非单维度必须彼此匹配

Any idea why this error? 知道为什么会出现此错误吗? Thanks! 谢谢!

you are trying to apply bsxfun on two column vectors, while you should apply it on one row and one column vector. 您试图将bsxfun应用于两个列向量,而应将其应用于一行和一列向量。

bsxfun inputs should have different singelton dimensions ( size(arr,dim) == 1 ). bsxfun输入应具有不同的 singelton尺寸( size(arr,dim) == 1 )。 in your example size(A) = [5472,1] and size(B) = [1,3] which is appropriate input ( A 's singelton dimension is 2 and B 's singelton dimension is 1), but when you do B(:) - B 's singelton dimension becomes 2, like A 's, which throws an error. 在您的示例中size(A) = [5472,1]size(B) = [1,3] ,这是合适的输入( A的singelton尺寸为2, B的singelton尺寸为1),但是当您这样做时B(:) - B的singelton维数变为2,就像A一样,这会引发错误。

do: 做:

A=rand(5472,1);
B=rand(1,3);

C= bsxfun(@minus, A, B)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM