Maximinium
Maximinium

Reputation: 27

matlab vector addition like multiplication, without for loop

Normally when one adds two vectors this is what happens

[1 4] + [2 5]  = [3 9]

I want it to do this:

[1 4] + [2 5]  =  3  6
                  6  9

So basically addition like how multiplication happens. But without using for-loops. Thanks so much!

Upvotes: 0

Views: 258

Answers (2)

Jonas
Jonas

Reputation: 74940

This is one of the poster cases for using bsxfun

x = [1 4];
y = [2 5];
bsxfun(@plus,x,y')

Upvotes: 3

shoelzer
shoelzer

Reputation: 10708

One way to do it is with meshgrid.

x = [1 4];
y = [2 5];
[a, b] = meshgrid(y,x);
a + b

Upvotes: 0

Related Questions