Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Subtraction of arrays - each element by each element

Subject: Subtraction of arrays - each element by each element

From: Milos Milenkovic

Date: 12 Jun, 2009 08:20:19

Message: 1 of 7

How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
a=[1 10 20]
b=[3 10 30]
c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
Thanks!

Subject: Subtraction of arrays - each element by each element

From: Jos

Date: 12 Jun, 2009 08:36:01

Message: 2 of 7

"Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t343$hf0$1@fred.mathworks.com>...
> How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> a=[1 10 20]
> b=[3 10 30]
> c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
> Thanks!

A perfect job for bsxfun!

a = [1 10 30]
b = [3 10 30]
c = bsxfun(@minus, a.',b)

hth
Jos

Subject: Subtraction of arrays - each element by each element

From: Milos Milenkovic

Date: 12 Jun, 2009 08:48:01

Message: 3 of 7

"Jos " <#10584@fileexchange.com> wrote in message <h0t41g$pfr$1@fred.mathworks.com>...
> "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t343$hf0$1@fred.mathworks.com>...
> > How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> > a=[1 10 20]
> > b=[3 10 30]
> > c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
> > Thanks!
>
> A perfect job for bsxfun!
>
> a = [1 10 30]
> b = [3 10 30]
> c = bsxfun(@minus, a.',b)
>
> hth
> Jos

Thank you Jos,
but I am affraid that I don't have this function in Matlab 7 version

Subject: Subtraction of arrays - each element by each element

From: Siyi

Date: 12 Jun, 2009 09:02:00

Message: 4 of 7

On Jun 12, 1:20 am, "Milos Milenkovic" <m.milenko...@mathworks.com>
wrote:
> How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> a=[1 10 20]
> b=[3 10 30]
> c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
> Thanks!



[a1,b1] = meshgrid(a,b)
c = a1-b1

Subject: Subtraction of arrays - each element by each element

From: Jos

Date: 12 Jun, 2009 09:02:01

Message: 5 of 7

"Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t4o1$fpu$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <h0t41g$pfr$1@fred.mathworks.com>...
> > "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t343$hf0$1@fred.mathworks.com>...
> > > How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> > > a=[1 10 20]
> > > b=[3 10 30]
> > > c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
> > > Thanks!
> >
> > A perfect job for bsxfun!
> >
> > a = [1 10 30]
> > b = [3 10 30]
> > c = bsxfun(@minus, a.',b)
> >
> > hth
> > Jos
>
> Thank you Jos,
> but I am affraid that I don't have this function in Matlab 7 version

Then use REPMAT or NDGRID:

a = [1 10 30], b = [3 10 30]
[aa,bb] = ndgrid(a,b)
c = aa-bb

Jos

Subject: Subtraction of arrays - each element by each element

From: James Tursa

Date: 12 Jun, 2009 18:15:18

Message: 6 of 7

"Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t4o1$fpu$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <h0t41g$pfr$1@fred.mathworks.com>...
> > "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <h0t343$hf0$1@fred.mathworks.com>...
> > > How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> > > a=[1 10 20]
> > > b=[3 10 30]
> > > c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]
> > > Thanks!
> >
> > A perfect job for bsxfun!
> >
> > a = [1 10 30]
> > b = [3 10 30]
> > c = bsxfun(@minus, a.',b)
> >
> > hth
> > Jos
>
> Thank you Jos,
> but I am affraid that I don't have this function in Matlab 7 version

You can get a substitute bsxfun function here for your version of MATLAB:

http://www.mathworks.com/matlabcentral/fileexchange/23005

James Tursa

Subject: Subtraction of arrays - each element by each element

From: Jan Simon

Date: 12 Jun, 2009 19:59:01

Message: 7 of 7

Dear Milos Milenkovic!

> How to make a subtraction of two arrays so that new array consist of subtracted elements on each by each basis, like this:
> a=[1 10 20]
> b=[3 10 30]
> c=a-b=[-2 -9 -29; 7 0 -20; 17 10 -10]

REPMAT, MESHGRID and NDGRID call ONES implicitely to inflate the missing dimension, so if you need speed, call it directly:
  aT = transpose(a);
  c = aT(:, ones(1, length(b))) - b(ones(1, length(aT)), :);

This is sometimes faster than BSXFUN (to my surprise!), if the JIT recgnizes the construction.

Good luck, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us