[英]bsxfun: Dimensions of matrices being concatenated are not consistent
Any one knows where the error is? 有人知道错误在哪里吗? Many thanks! 非常感谢!
beta=randn(50,1);
bsxfun(@(x1,x2) max([x1 x2 x1+x2]), beta, beta')
error message: 错误信息:
Error using horzcat 使用Horzcat时出错
Dimensions of matrices being concatenated are not consistent. 串联的矩阵尺寸不一致。
Error in @(x1,x2)max([x1,x2,x1+x2]) @(x1,x2)max([x1,x2,x1 + x2])中的错误
I'm not 100% sure what you want to achieve, but the error is in the transposition of beta
as third argument of bsxfun
; 我不是100%不确定要实现什么,但是错误在于将beta
为bsxfun
第三个参数; it works like this: 它是这样的:
beta=randn(50,1);
bsxfun(@(x1,x2) max([x1 x2 x1+x2]), beta, beta)
The second and third argument of bsxfun
needs to be of the same size to apply element-wise binary operations to it. bsxfun
的第二个和第三个参数的大小必须相同,才能对其应用按元素的二进制运算。
Edit: From the manual ( http://www.mathworks.de/de/help/matlab/ref/bsxfun.html ): 编辑:从手册( http://www.mathworks.de/de/help/matlab/ref/bsxfun.html ):
fun can also be a handle to any binary element-wise function not listed above. fun也可以是上面未列出的任何二进制按元素函数的句柄。 A binary element-wise function of the form C = fun(A,B) accepts arrays A and B of arbitrary, but equal size and returns output of the same size. 形式为C = fun(A,B)的二进制按元素函数接受任意但大小相等的数组A和B,并返回相同大小的输出。 Each element in the output array C is the result of an operation on the corresponding elements of A and B only. 输出数组C中的每个元素仅是对A和B的相应元素进行运算的结果。
EDIT2 : Is this, what you want? EDIT2 :这是您想要的吗?
A = rand(1,50);
[x, y] = ndgrid(1:length(A), 1:length(A));
idc = [x(:) y(:)];
allMin = min([A(idc(:,1)) A(idc(:,2)) A(idc(:,1))+A(idc(:,2))]);
First, with the second and third code-line I generate all possible combinations of indices (all pairs i
/ j
), eg: If A
has 3 entries, idc
would look like: 首先,使用第二和第三条代码行生成索引的所有可能组合(所有对i
/ j
),例如:如果A
有3个条目,则idc
看起来像:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
and then I'm building up a vector containing the value A(i)
, A(j)
and A(i)+A(j)
for each row of entries ( i
, j
) and getting the min
of it. 然后我为每个条目行( i
, j
)建立一个包含值A(i)
, A(j)
和A(i)+A(j)
的向量,并获取其min
。
Here's what I got (using two max
in bsxfun
) 这就是我得到的(在bsxfun
使用两个max
)
beta = randn(50,1);
res = bsxfun(@(x,y) max( x, max(y, x+y) ), beta, beta');
Verifying using repmat
使用repmat
验证
tmp = max( cat(3, repmat(beta,[1 50]), repmat(beta',[50 1]), ...
repmat(beta,[1 50])+ repmat(beta',[50 1]) ), [], 3);
isequal( tmp, res )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.