Question

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
Dimensions of matrices being concatenated are not consistent.
Error in @(x1,x2)max([x1,x2,x1+x2])

Was it helpful?

Solution 2

Here's what I got (using two max in bsxfun)

beta = randn(50,1); 
res = bsxfun(@(x,y) max( x, max(y, x+y) ), beta, beta');

Verifying using 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 )

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top