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:
subtract vector from matrix rows without for loop

Subject: subtract vector from matrix rows without for loop

From: Patrick Diviacco

Date: 29 Nov, 2009 02:26:04

Message: 1 of 8

hi,

can I subtract the same vector to each row of a matrix without using a for loop ?
thanks

Subject: subtract vector from matrix rows without for loop

From: ade77

Date: 29 Nov, 2009 02:49:04

Message: 2 of 8

"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesm3s$3sn$1@fred.mathworks.com>...
> hi,
>
> can I subtract the same vector to each row of a matrix without using a for loop ?
> thanks

yes

Subject: subtract vector from matrix rows without for loop

From: Matt Fig

Date: 29 Nov, 2009 02:53:03

Message: 3 of 8

% Data
A = magic(3);
B = [ 1 2 3];

% Engine 1, good for large arrays when memory is an issue.
C = bsxfun(@minus,A,B);

% Engine 2, may be faster for small arrays.
C = A - B(ones(size(A,1),1),:);

Subject: subtract vector from matrix rows without for loop

From: Matt J

Date: 29 Nov, 2009 04:54:03

Message: 4 of 8

"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesm3s$3sn$1@fred.mathworks.com>...
> hi,
>
> can I subtract the same vector to each row of a matrix without using a for loop ?
> thanks

For loops may be optimal (looping along the smallest dimension of the matrix), when the smallest dimension is small enough.

Subject: subtract vector from matrix rows without for loop

From: Matt J

Date: 29 Nov, 2009 05:10:24

Message: 5 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hesupb$i07$1@fred.mathworks.com>...
 
> For loops may be optimal (looping along the smallest dimension of the matrix), when the smallest dimension is small enough.
==================

In fact, for loops _are_ optimal, in terms of memory consumption, if you want to the new matrix to overwrite the old matrix. For example

M=bsxfun(@minus,M,vector);

will cause bsxfun() to generate an intermediate matrix the same size as M and then overwrite M with this intermediate matrix . Conversely, the following for-loop will allocate no new memory:

for ii=1:n
  M(:,ii)=M(:,ii)-vector;
end

Subject: subtract vector from matrix rows without for loop

From: Remus

Date: 7 Dec, 2012 04:48:15

Message: 6 of 8

"Matt J" wrote in message <hesupb$i07$1@fred.mathworks.com>...
> "Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesm3s$3sn$1@fred.mathworks.com>...
> > hi,
> >
> > can I subtract the same vector to each row of a matrix without using a for loop ?
> > thanks
>
> For loops may be optimal (looping along the smallest dimension of the matrix), when the smallest dimension is small enough.

I like Matt's answer... here is another way of doing it, slightly more efficient.

A = magic(3);
B = [ 1 2 3];
C = A-diag(B)*ones(size(A));

In essence is the same thing.

Subject: subtract vector from matrix rows without for loop

From: Remus

Date: 7 Dec, 2012 04:49:15

Message: 7 of 8

"Matt J" wrote in message <hesupb$i07$1@fred.mathworks.com>...
> "Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesm3s$3sn$1@fred.mathworks.com>...
> > hi,
> >
> > can I subtract the same vector to each row of a matrix without using a for loop ?
> > thanks
>
> For loops may be optimal (looping along the smallest dimension of the matrix), when the smallest dimension is small enough.

I like Matt's answer... here is another way of doing it, slightly more efficient.

A = magic(3);
B = [ 1 2 3];
C = A-diag(B)*ones(size(A));

In essence is the same thing.

Subject: subtract vector from matrix rows without for loop

From: Ziyad

Date: 5 May, 2013 20:41:08

Message: 8 of 8

and what if you want to divide the vector elements from each row of the matrix

Tags for this Thread

No tags are associated with 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