Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!news.glorb.com!news2.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!newsfe04.iad.POSTED!7564ea0f!not-for-mail
From: "Nasser M. Abbasi" <nma@12000.org>
Newsgroups: comp.soft-sys.matlab
References: <hcsd27$15e$1@fred.mathworks.com>
Subject: Re: adding a vector to each col of a previous vector
Lines: 49
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3598
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
X-RFC2646: Format=Flowed; Original
X-EsetId: 321EA926BF2033396652
X-EsetScannerBuild: 5969
Message-ID: <QXiIm.7585$fE2.5316@newsfe04.iad>
NNTP-Posting-Host: ncdeodfefpjopplmihjclpliaacepnnh
X-Complaints-To: abuse@charter.net
X-Trace: ifnckfofmjglplgfadefjppgkgeilljafafmdepdnpnfaklmncdeodfefpjopplmmgdmmdanmgdmojgmpfpilbjnipcaofjcildolkkeocphpjlefilopaebclobhbjnomehlggnafaaoamp
NNTP-Posting-Date: Wed, 04 Nov 2009 17:35:44 UTC
Date: Wed, 4 Nov 2009 11:35:43 -0600
Xref: news.mathworks.com comp.soft-sys.matlab:582460



"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