我需要创建一个矩阵,其中我有0-1范围内的点,模拟xor门,如图所示,bu点也应存在于左上角和右下角。
我正在使用此代码:
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);
答案 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列。