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:
subsctract two column vectors without for loop

Subject: subsctract two column vectors without for loop

From: Edwin

Date: 12 Aug, 2010 08:15:23

Message: 1 of 5

Hello everyone,
if I have two column vector for example
gm=[1:1:100]';
g=[1:1:21]';

Could someone please show me how can I subtract each row in gm by each row in g,
so I can get a new matrix with size 100x21 without using a for loop.


Regards

Subject: subsctract two column vectors without for loop

From: Carl

Date: 12 Aug, 2010 10:43:23

Message: 2 of 5

"Edwin " <onest30@gmail.com> wrote in message <i40air$grs$1@fred.mathworks.com>...
> Hello everyone,
> if I have two column vector for example
> gm=[1:1:100]';
> g=[1:1:21]';
>
> Could someone please show me how can I subtract each row in gm by each row in g,
> so I can get a new matrix with size 100x21 without using a for loop.
>
>
> Regards

Have a look at repmat you should be able to work out what to do from there.

Subject: subsctract two column vectors without for loop

From: Edwin

Date: 12 Aug, 2010 10:48:05

Message: 3 of 5

Thanks

just in case, this is what I did

Z=repmat(g,[1 size(gm,1)])-(ones(size(g,1),1))*gm';


Regards

Subject: subsctract two column vectors without for loop

From: Carl

Date: 12 Aug, 2010 13:04:08

Message: 4 of 5

"Edwin " <onest30@gmail.com> wrote in message <i40air$grs$1@fred.mathworks.com>...
> Hello everyone,
> if I have two column vector for example
> gm=[1:1:100]';
> g=[1:1:21]';
>
> Could someone please show me how can I subtract each row in gm by each row in g,
> so I can get a new matrix with size 100x21 without using a for loop.
>
>
> Regards

Have a look at repmat you should be able to work out what to do from there.

Subject: subsctract two column vectors without for loop

From: Matt J

Date: 12 Aug, 2010 14:43:08

Message: 5 of 5



Use BSXFUN

 bsxfun(@minus,gm,g.');


Do not use REPMAT. It costs more memory than is necessary here.

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