Matlab,矩阵尺寸必须一致

时间:2015-04-25 14:23:14

标签: matlab matrix

我需要创建一个矩阵,其中我有0-1范围内的点,模拟xor门,如图所示,bu点也应存在于左上角和右下角。

enter image description here

我正在使用此代码:

    pats = [0.4*rand(n/4,2);
            0.4*rand(n/4,2)+0.6;
            0.4*rand(n/4,2)+[0 0.5];
            0.4*rand(n/4,2)-[0 0.5]+0.6];

我收到以下错误:

Warning: Size vector should be a row
vector with integer elements. 
> In main at 24 
Warning: Size vector should be a row
vector with integer elements. 
> In main at 24 
Warning: Size vector should be a row
vector with integer elements. 
> In main at 24 
Warning: Size vector should be a row
vector with integer elements. 
> In main at 24 
Error using  + 
Matrix dimensions must agree.

Error in main (line 24)
        pats = [0.4*rand(n/4,2);

1 个答案:

答案 0 :(得分:2)

元素明智的添加对您的randn(n/4,2) + [0 0.5]不起作用,因为您试图将n/4乘2矩阵添加到1乘2矩阵。您需要使用bsxfun

pats = [0.4*rand(n/4,2);
    0.4*rand(n/4,2)+0.6;
    bsxfun(@plus,0.4*rand(n/4,2),[0 0.5]);
    bsxfun(@minus,0.4*rand(n/4,2),[0 0.5]) + 0.6];

函数bsxfun(@plus, A, b)会将b的第i个元素添加到矩阵A的第i列。