Asked by Matt J
on 2 Jun 2013

I don't think the pipe symbol '|' is being used for anything in MATLAB syntax. Would it be a good idea to use it to make a family of BSXFUN operators? For example, instead of

C=bsxfun(@times,A,B)

C=bsxfun(@plus,A,B)

you could do things like

C=A|*B;

C=A|+B;

Answer by Walter Roberson
on 2 Jun 2013

Edited by Walter Roberson
on 2 Jun 2013

Pipe is "or"

Unused symbols are: back-quote (`), octothorp (#), dollar-sign ($), and question-mark (?). Oh yes, and double-quote (")

Answer by Jan
on 2 Jun 2013

James Tursa
on 2 Jun 2013

Matt J
on 2 Jun 2013

I'm not seeing the backward compatibility issue. Since these operators would be new, there's no reason old code pre-dating these operators would have a problem running. I'm not suggesting they take BSXFUN away, just open new syntaxes for calling it.

The approach of using new classes is one I've tried, see for example,

It's not a graceful solution, though, because you have to overload all stock Matlab functions for them, SORT, SIN, COS, TRIU,... Subclassing built-in classes doesn't help. The output of many builtin MATLAB functions (e.g., TRIU, TRIL) silently convert subclass input back to doubles and it's painful to keep track of this.

Sign in to comment.

Answer by Knut
on 2 Jun 2013

Edited by Knut
on 2 Jun 2013

I occasionally use the kron() function, and I probably should use bsxfun() more. What I seem to be banging my head up against: I saw the light with MATLAB when I grasped the beauty and compactness of regular 2-d oriented array/vectorization. The fact that it tended to make my MATLAB scripts faster did not hurt either.

Recently, I am feeling constrained: for the kind of stuff that I am doing, MATLAB vectorization can only take me so far. I believe that what I am longing for is tensor math. Is this a direction that TMW will/should head for, or is it too exotic/hard to make fast libraries/something else?

I'd like to write:

a = ones(2,4,3);

g = cat(3,1,2,3);

%1. short-hand for:

%for t = 1:3,

% R1(:,:,t)=a(:,:,t)*g(t);

%end

R1 = a.*g;

R1(:,:,1) =

1 1 1 1

1 1 1 1

R1(:,:,2) =

2 2 2 2

2 2 2 2

R1(:,:,3) =

3 3 3 3

3 3 3 3

%2. short-hand for:

%R2 = zeros(2,4);

%for t = 1:3,

% R2=R2+a(:,:,t)*g(t);

%end

R2 = a*g;

R2 =

6 6 6 6

6 6 6 6

Matt J
on 2 Jun 2013

Hi Knut. Yes, I take your point. However, you know, I hope, that your first example is vectorizable via BSXFUN

R1=bsxfun(@times,a,g)

even though, as I've been saying, it would be nice to have a shorter syntax for this.

The second example can be done with KRON, which I guess is what you've been doing. Note that the KronProd class can be a more efficient alternative to kron(), and somewhat approximates the shorter syntax you desire:

>> g=KronProd({1,1,[1,2,3]},[1,2,3],size(a));

>> g*a

ans =

6 6 6 6

6 6 6 6

This is an approach more along the lines of what Jan and James were recommending, using a class capable of tensor operations. I certainly agree that it could be handy to have a built-in operator syntax for this, though.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Matt J (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/77713-would-a-bsxfun-operator-family-be-a-good-idea#comment_152394

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/77713-would-a-bsxfun-operator-family-be-a-good-idea#comment_152395

## Matt J (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/77713-would-a-bsxfun-operator-family-be-a-good-idea#comment_152399

## Walter Roberson (view profile)

Direct link to this comment:https://it.mathworks.com/matlabcentral/answers/77713-would-a-bsxfun-operator-family-be-a-good-idea#comment_152496

Sign in to comment.