Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!r24g2000prf.googlegroups.com!not-for-mail
From: Nathan <ngreco32@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: adding a vector to each col of a previous vector
Date: Wed, 4 Nov 2009 10:07:32 -0800 (PST)
Organization: http://groups.google.com
Lines: 75
Message-ID: <ef0048a5-3edd-4bad-b515-0126205943d9@r24g2000prf.googlegroups.com>
References: <hcsd27$15e$1@fred.mathworks.com> <QXiIm.7585$fE2.5316@newsfe04.iad>
NNTP-Posting-Host: 198.206.219.34
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1257358053 14135 127.0.0.1 (4 Nov 2009 18:07:33 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 4 Nov 2009 18:07:33 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: r24g2000prf.googlegroups.com; posting-host=198.206.219.34; 
	posting-account=_KeVcAoAAAB7j3xn35ujaQ0BoQhuzwJP
User-Agent: G2/1.0
X-HTTP-Via: 1.1 wwwproxy-son-ca-02.ca.sandia.gov:80 (squid/2.5.STABLE14)
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) 
	Gecko/20091016 Firefox/3.5.4,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:582471


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