Got Questions? Get Answers.
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:
arimethic , without for-loop construction

Subject: arimethic , without for-loop construction

From: Stefan

Date: 26 Jul, 2010 15:14:04

Message: 1 of 5

Hello,

I would like to subtract each element of a from array b.
a=[2 4 5 1 30 4]
b=[12 32 87 56 45 67 13]
Is there a way to solve this problem without using a for-loop?
Thanks

ex: Subtracting 3rd element of a from array b
5 - [12 32 87 56 45 67 13] = [-7 -27 -82 -51 -40 -62 -8]

Subject: arimethic , without for-loop construction

From: us

Date: 26 Jul, 2010 15:26:08

Message: 2 of 5

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2k8ns$cdf$1@fred.mathworks.com>...
> Hello,
>
> I would like to subtract each element of a from array b.
> a=[2 4 5 1 30 4]
> b=[12 32 87 56 45 67 13]
> Is there a way to solve this problem without using a for-loop?
> Thanks
>
> ex: Subtracting 3rd element of a from array b
> 5 - [12 32 87 56 45 67 13] = [-7 -27 -82 -51 -40 -62 -8]

one of the solutions

     a=1:5;
     b=1:5;
     r=a(3)-b
% r = 2 1 0 -1 -2

us

Subject: arimethic , without for-loop construction

From: Walter Roberson

Date: 26 Jul, 2010 15:28:20

Message: 3 of 5

Stefan wrote:
> Hello,
>
> I would like to subtract each element of a from array b.
> a=[2 4 5 1 30 4]
> b=[12 32 87 56 45 67 13]
> Is there a way to solve this problem without using a for-loop?
> Thanks
>
> ex: Subtracting 3rd element of a from array b
> 5 - [12 32 87 56 45 67 13] = [-7 -27 -82 -51 -40 -62 -8]

bsxfun(@minus, b, a.')

Subject: arimethic , without for-loop construction

From: Fangjun Jiang

Date: 26 Jul, 2010 15:30:23

Message: 4 of 5

"Stefan " <aidematlab@yahoo.ca> wrote in message <i2k8ns$cdf$1@fred.mathworks.com>...
> Hello,
>
> I would like to subtract each element of a from array b.
> a=[2 4 5 1 30 4]
> b=[12 32 87 56 45 67 13]
> Is there a way to solve this problem without using a for-loop?
> Thanks
>
> ex: Subtracting 3rd element of a from array b
> 5 - [12 32 87 56 45 67 13] = [-7 -27 -82 -51 -40 -62 -8]

>> repmat(a',1,size(b,2))-repmat(b,size(a,2),1)

ans =

   -10 -30 -85 -54 -43 -65 -11
    -8 -28 -83 -52 -41 -63 -9
    -7 -27 -82 -51 -40 -62 -8
   -11 -31 -86 -55 -44 -66 -12
    18 -2 -57 -26 -15 -37 17
    -8 -28 -83 -52 -41 -63 -9

Subject: arimethic , without for-loop construction

From: us

Date: 26 Jul, 2010 15:49:03

Message: 5 of 5

"Fangjun Jiang" <f@j.com> wrote in message <i2k9mf$f7f$1@fred.mathworks.com>...
> "Stefan " <aidematlab@yahoo.ca> wrote in message <i2k8ns$cdf$1@fred.mathworks.com>...
> > Hello,
> >
> > I would like to subtract each element of a from array b.
> > a=[2 4 5 1 30 4]
> > b=[12 32 87 56 45 67 13]
> > Is there a way to solve this problem without using a for-loop?
> > Thanks
> >
> > ex: Subtracting 3rd element of a from array b
> > 5 - [12 32 87 56 45 67 13] = [-7 -27 -82 -51 -40 -62 -8]
>
> >> repmat(a',1,size(b,2))-repmat(b,size(a,2),1)
>
> ans =
>
> -10 -30 -85 -54 -43 -65 -11
> -8 -28 -83 -52 -41 -63 -9
> -7 -27 -82 -51 -40 -62 -8
> -11 -31 -86 -55 -44 -66 -12
> 18 -2 -57 -26 -15 -37 17
> -8 -28 -83 -52 -41 -63 -9

firstly, i came up with a bad solution because i didn't read the OP carefully enough - sorry...
second, walter's solution using BSXFUN is the way to go...

us

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