Thread Subject: adding a vector to each col of a previous vector

Subject: adding a vector to each col of a previous vector

From: Adrian

Date: 4 Nov, 2009 17:19:03

Message: 1 of 5

I use too many loops all the time and I want to become more familiar with vector notation and matrix manipulation.
So I would like to know the following.
Without using a loop what is the most elegant (and efficient) way to add a new vector to each col of another vector.
For example:
a = [1 5 20 35 100]
b = 0:4;

c = [1 5 20 35 100
       2 6 21 36 101
       3 7 22 37 102
       4 8 23 38 103
       5 9 24 39 104]

This is probably pretty easy. Help me out if you can.

Subject: adding a vector to each col of a previous vector

From: Matt

Date: 4 Nov, 2009 17:31:04

Message: 2 of 5

"Adrian " <adrian.suszko@uhn.on.ca> wrote in message <hcsd27$15e$1@fred.mathworks.com>...
> I use too many loops all the time and I want to become more familiar with vector notation and matrix manipulation.
> So I would like to know the following.
> Without using a loop what is the most elegant (and efficient) way to add a new vector to each col of another vector.
> For example:
> a = [1 5 20 35 100]
> b = 0:4;
>
> c = [1 5 20 35 100
> 2 6 21 36 101
> 3 7 22 37 102
> 4 8 23 38 103
> 5 9 24 39 104]
>
> This is probably pretty easy. Help me out if you can.


The easiest is bsxfun(@plus, b',a);

If even only one of a or b has small dimensions, however, for-loops have been found to be more efficient.

Subject: adding a vector to each col of a previous vector

From: Nasser M. Abbasi

Date: 4 Nov, 2009 17:35:43

Message: 3 of 5


"Adrian " <adrian.suszko@uhn.on.ca> wrote in message
news:hcsd27$15e$1@fred.mathworks.com...
>I use too many loops all the time and I want to become more familiar with
>vector notation and matrix manipulation.
> So I would like to know the following.
> Without using a loop what is the most elegant (and efficient) way to add a
> new vector to each col of another vector.

The name vector is usually used for stuff that has one row or column. For
stuff with more than one of these, it is called a matrix, not a vector.
Altough a vector can also be considered a matrix, but normally we call a
matrix something that has more than one row or column in it. So 'c' here is
a matrix not a vector.

> For example:
> a = [1 5 20 35 100]
> b = 0:4;
>
> c = [1 5 20 35 100
> 2 6 21 36 101
> 3 7 22 37 102
> 4 8 23 38 103
> 5 9 24 39 104]
>
> This is probably pretty easy. Help me out if you can.

To add 'a' to bottom of c matrix, one way is to repmat it below c as follows

[nRow,nCol]=size(c);
newc=[c;repmat(a',[1 nCol])]

newc =
     1 5 20 35 100
     2 6 21 36 101
     3 7 22 37 102
     4 8 23 38 103
     5 9 24 39 104
     1 1 1 1 1
     5 5 5 5 5
    20 20 20 20 20
    35 35 35 35 35
   100 100 100 100 100

similary for b

--Nasser

Subject: adding a vector to each col of a previous vector

From: Nathan

Date: 4 Nov, 2009 18:07:32

Message: 4 of 5

On Nov 4, 9:35 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> "Adrian " <adrian.sus...@uhn.on.ca> wrote in message
>
> news:hcsd27$15e$1@fred.mathworks.com...
>
> >I use too many loops all the time and I want to become more familiar with
> >vector notation and matrix manipulation.
> > So I would like to know the following.
> > Without using a loop what is the most elegant (and efficient) way to add a
> > new vector to each col of another vector.
>
> The name vector is usually used for stuff that has one row or column. For
> stuff with more than one of these, it is called a matrix, not a vector.
> Altough a vector can also be considered a matrix, but normally we call a
> matrix something that has more than one row or column in it. So 'c' here is
> a matrix not a vector.
>
> > For example:
> > a = [1 5 20 35 100]
> > b = 0:4;
>
> > c = [1 5 20 35 100
> >       2 6 21 36 101
> >       3 7 22 37 102
> >       4 8 23 38 103
> >       5 9 24 39 104]
>
> > This is probably pretty easy. Help me out if you can.
>
> To add 'a' to bottom of c matrix, one way is to repmat it below c as follows
>
> [nRow,nCol]=size(c);
> newc=[c;repmat(a',[1 nCol])]
>
> newc =
>      1     5    20    35   100
>      2     6    21    36   101
>      3     7    22    37   102
>      4     8    23    38   103
>      5     9    24    39   104
>      1     1     1     1     1
>      5     5     5     5     5
>     20    20    20    20    20
>     35    35    35    35    35
>    100   100   100   100   100
>
> similary for b
>
> --Nasser

I think he meant from a and b, he gets c
ex:
c = [a+b(1);a+b(2);a+b(3);a+b(4);a+b(5)]
%%%%%%%%%%%%%%%%%%%%%%%
c =
     1 5 20 35 100
     2 6 21 36 101
     3 7 22 37 102
     4 8 23 38 103
     5 9 24 39 104

I dunno. I think for loops work wonderfully for this.

initialize c:
c = zeros(length(a),length(b));
for i=1:length(b)
c(i,:) = a+b(i)
end


-Nathan

Subject: adding a vector to each col of a previous vector

From: Adrian

Date: 4 Nov, 2009 18:10:19

Message: 5 of 5

"Nasser M. Abbasi" <nma@12000.org> wrote in message <QXiIm.7585$fE2.5316@newsfe04.iad>...
>
> "Adrian " <adrian.suszko@uhn.on.ca> wrote in message
> news:hcsd27$15e$1@fred.mathworks.com...
> >I use too many loops all the time and I want to become more familiar with
> >vector notation and matrix manipulation.
> > So I would like to know the following.
> > Without using a loop what is the most elegant (and efficient) way to add a
> > new vector to each col of another vector.
>
> The name vector is usually used for stuff that has one row or column. For
> stuff with more than one of these, it is called a matrix, not a vector.
> Altough a vector can also be considered a matrix, but normally we call a
> matrix something that has more than one row or column in it. So 'c' here is
> a matrix not a vector.
>
> > For example:
> > a = [1 5 20 35 100]
> > b = 0:4;
> >
> > c = [1 5 20 35 100
> > 2 6 21 36 101
> > 3 7 22 37 102
> > 4 8 23 38 103
> > 5 9 24 39 104]
> >
> > This is probably pretty easy. Help me out if you can.
>
> To add 'a' to bottom of c matrix, one way is to repmat it below c as follows
>
> [nRow,nCol]=size(c);
> newc=[c;repmat(a',[1 nCol])]
>
> newc =
> 1 5 20 35 100
> 2 6 21 36 101
> 3 7 22 37 102
> 4 8 23 38 103
> 5 9 24 39 104
> 1 1 1 1 1
> 5 5 5 5 5
> 20 20 20 20 20
> 35 35 35 35 35
> 100 100 100 100 100
>
> similary for b
>
> --Nasser
>
Apologies I rushed that post and didn't include the fact that I meant that I wanted my result to be a matrix which in this case was the "c" that i described above.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vector matrix m... Adrian 4 Nov, 2009 12:24:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com