有谁知道错误在哪里?非常感谢!
beta=randn(50,1);
bsxfun(@(x1,x2) max([x1 x2 x1+x2]), beta, beta')
错误信息:
使用 horzcat 时出错
被连接的矩阵的维度不一致。
@(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
; 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.
Edit: From the manual (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. 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. Each element in the output array C is the result of an operation on the corresponding elements of A and B only.
EDIT2: Is this, what you want?
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
), e.g.: If A
has 3 entries, idc
would look like:
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.
这是我得到的(使用两个max
in bsxfun
)
beta = randn(50,1);
res = bsxfun(@(x,y) max( x, max(y, x+y) ), beta, beta');
验证使用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 )