<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992</link>
    <title>MATLAB Central Newsreader - adding a vector to each col of a previous vector</title>
    <description>Feed for thread: adding a vector to each col of a previous vector</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 04 Nov 2009 17:19:03 -0500</pubDate>
      <title>adding a vector to each col of a previous vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992#692121</link>
      <author>Adrian </author>
      <description>I use too many loops all the time and I want to become more familiar with vector notation and matrix manipulation.&lt;br&gt;
So I would like to know the following.&lt;br&gt;
Without using a loop what is the most elegant (and efficient) way to add a new vector to each col of another vector.&lt;br&gt;
For example:&lt;br&gt;
a = [1 5 20 35 100]&lt;br&gt;
b = 0:4;&lt;br&gt;
&lt;br&gt;
c = [1 5 20 35 100&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2 6 21 36 101&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3 7 22 37 102&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4 8 23 38 103&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5 9 24 39 104]&lt;br&gt;
&lt;br&gt;
This is probably pretty easy. Help me out if you can.</description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 17:31:04 -0500</pubDate>
      <title>Re: adding a vector to each col of a previous vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992#692128</link>
      <author>Matt </author>
      <description>&quot;Adrian &quot; &amp;lt;adrian.suszko@uhn.on.ca&amp;gt; wrote in message &amp;lt;hcsd27$15e$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I use too many loops all the time and I want to become more familiar with vector notation and matrix manipulation.&lt;br&gt;
&amp;gt; So I would like to know the following.&lt;br&gt;
&amp;gt; Without using a loop what is the most elegant (and efficient) way to add a new vector to each col of another vector.&lt;br&gt;
&amp;gt; For example:&lt;br&gt;
&amp;gt; a = [1 5 20 35 100]&lt;br&gt;
&amp;gt; b = 0:4;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; c = [1 5 20 35 100&lt;br&gt;
&amp;gt;        2 6 21 36 101&lt;br&gt;
&amp;gt;        3 7 22 37 102&lt;br&gt;
&amp;gt;        4 8 23 38 103&lt;br&gt;
&amp;gt;        5 9 24 39 104]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This is probably pretty easy. Help me out if you can.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The easiest is bsxfun(@plus, b',a);&lt;br&gt;
&lt;br&gt;
If even only one of a or b has small dimensions, however, for-loops have been found to be more efficient.</description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 17:35:43 -0500</pubDate>
      <title>Re: adding a vector to each col of a previous vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992#692129</link>
      <author>Nasser M. Abbasi</author>
      <description>&lt;br&gt;
&quot;Adrian &quot; &amp;lt;adrian.suszko@uhn.on.ca&amp;gt; wrote in message &lt;br&gt;
news:hcsd27$15e$1@fred.mathworks.com...&lt;br&gt;
&amp;gt;I use too many loops all the time and I want to become more familiar with &lt;br&gt;
&amp;gt;vector notation and matrix manipulation.&lt;br&gt;
&amp;gt; So I would like to know the following.&lt;br&gt;
&amp;gt; Without using a loop what is the most elegant (and efficient) way to add a &lt;br&gt;
&amp;gt; new vector to each col of another vector.&lt;br&gt;
&lt;br&gt;
The name vector is usually used for stuff that has one row or column. For &lt;br&gt;
stuff with more than one of these, it is called a matrix, not a vector. &lt;br&gt;
Altough a vector can also be considered a matrix, but normally we call a &lt;br&gt;
matrix something that has more than one row or column in it. So 'c' here is &lt;br&gt;
a matrix not a vector.&lt;br&gt;
&lt;br&gt;
&amp;gt; For example:&lt;br&gt;
&amp;gt; a = [1 5 20 35 100]&lt;br&gt;
&amp;gt; b = 0:4;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; c = [1 5 20 35 100&lt;br&gt;
&amp;gt;       2 6 21 36 101&lt;br&gt;
&amp;gt;       3 7 22 37 102&lt;br&gt;
&amp;gt;       4 8 23 38 103&lt;br&gt;
&amp;gt;       5 9 24 39 104]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This is probably pretty easy. Help me out if you can.&lt;br&gt;
&lt;br&gt;
To add 'a' to bottom of c matrix, one way is to repmat it below c as follows&lt;br&gt;
&lt;br&gt;
[nRow,nCol]=size(c);&lt;br&gt;
newc=[c;repmat(a',[1 nCol])]&lt;br&gt;
&lt;br&gt;
newc =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     5    20    35   100&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     6    21    36   101&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     7    22    37   102&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     8    23    38   103&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     9    24    39   104&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     1     1     1     1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     5     5     5     5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20    20    20    20    20&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;35    35    35    35    35&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;100   100   100   100   100&lt;br&gt;
&lt;br&gt;
similary for b&lt;br&gt;
&lt;br&gt;
--Nasser </description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 18:07:32 -0500</pubDate>
      <title>Re: adding a vector to each col of a previous vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992#692140</link>
      <author>Nathan</author>
      <description>On Nov 4, 9:35&#160;am, &quot;Nasser M. Abbasi&quot; &amp;lt;n...@12000.org&amp;gt; wrote:&lt;br&gt;
&amp;gt; &quot;Adrian &quot; &amp;lt;adrian.sus...@uhn.on.ca&amp;gt; wrote in message&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; news:hcsd27$15e$1@fred.mathworks.com...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;I use too many loops all the time and I want to become more familiar with&lt;br&gt;
&amp;gt; &amp;gt;vector notation and matrix manipulation.&lt;br&gt;
&amp;gt; &amp;gt; So I would like to know the following.&lt;br&gt;
&amp;gt; &amp;gt; Without using a loop what is the most elegant (and efficient) way to add a&lt;br&gt;
&amp;gt; &amp;gt; new vector to each col of another vector.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The name vector is usually used for stuff that has one row or column. For&lt;br&gt;
&amp;gt; stuff with more than one of these, it is called a matrix, not a vector.&lt;br&gt;
&amp;gt; Altough a vector can also be considered a matrix, but normally we call a&lt;br&gt;
&amp;gt; matrix something that has more than one row or column in it. So 'c' here is&lt;br&gt;
&amp;gt; a matrix not a vector.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; For example:&lt;br&gt;
&amp;gt; &amp;gt; a = [1 5 20 35 100]&lt;br&gt;
&amp;gt; &amp;gt; b = 0:4;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; c = [1 5 20 35 100&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160; 2 6 21 36 101&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160; 3 7 22 37 102&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160; 4 8 23 38 103&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160; 5 9 24 39 104]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; This is probably pretty easy. Help me out if you can.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; To add 'a' to bottom of c matrix, one way is to repmat it below c as follows&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [nRow,nCol]=size(c);&lt;br&gt;
&amp;gt; newc=[c;repmat(a',[1 nCol])]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; newc =&lt;br&gt;
&amp;gt; &#160; &#160; &#160;1 &#160; &#160; 5 &#160; &#160;20 &#160; &#160;35 &#160; 100&lt;br&gt;
&amp;gt; &#160; &#160; &#160;2 &#160; &#160; 6 &#160; &#160;21 &#160; &#160;36 &#160; 101&lt;br&gt;
&amp;gt; &#160; &#160; &#160;3 &#160; &#160; 7 &#160; &#160;22 &#160; &#160;37 &#160; 102&lt;br&gt;
&amp;gt; &#160; &#160; &#160;4 &#160; &#160; 8 &#160; &#160;23 &#160; &#160;38 &#160; 103&lt;br&gt;
&amp;gt; &#160; &#160; &#160;5 &#160; &#160; 9 &#160; &#160;24 &#160; &#160;39 &#160; 104&lt;br&gt;
&amp;gt; &#160; &#160; &#160;1 &#160; &#160; 1 &#160; &#160; 1 &#160; &#160; 1 &#160; &#160; 1&lt;br&gt;
&amp;gt; &#160; &#160; &#160;5 &#160; &#160; 5 &#160; &#160; 5 &#160; &#160; 5 &#160; &#160; 5&lt;br&gt;
&amp;gt; &#160; &#160; 20 &#160; &#160;20 &#160; &#160;20 &#160; &#160;20 &#160; &#160;20&lt;br&gt;
&amp;gt; &#160; &#160; 35 &#160; &#160;35 &#160; &#160;35 &#160; &#160;35 &#160; &#160;35&lt;br&gt;
&amp;gt; &#160; &#160;100 &#160; 100 &#160; 100 &#160; 100 &#160; 100&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; similary for b&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; --Nasser&lt;br&gt;
&lt;br&gt;
I think he meant from a and b, he gets c&lt;br&gt;
ex:&lt;br&gt;
c = [a+b(1);a+b(2);a+b(3);a+b(4);a+b(5)]&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
c =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     5    20    35   100&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     6    21    36   101&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     7    22    37   102&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     8    23    38   103&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5     9    24    39   104&lt;br&gt;
&lt;br&gt;
I dunno. I think for loops work wonderfully for this.&lt;br&gt;
&lt;br&gt;
initialize c:&lt;br&gt;
c = zeros(length(a),length(b));&lt;br&gt;
for i=1:length(b)&lt;br&gt;
c(i,:) = a+b(i)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Nathan</description>
    </item>
    <item>
      <pubDate>Wed, 04 Nov 2009 18:10:19 -0500</pubDate>
      <title>Re: adding a vector to each col of a previous vector</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264992#692141</link>
      <author>Adrian </author>
      <description>&quot;Nasser M. Abbasi&quot; &amp;lt;nma@12000.org&amp;gt; wrote in message &amp;lt;QXiIm.7585$fE2.5316@newsfe04.iad&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Adrian &quot; &amp;lt;adrian.suszko@uhn.on.ca&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:hcsd27$15e$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &amp;gt;I use too many loops all the time and I want to become more familiar with &lt;br&gt;
&amp;gt; &amp;gt;vector notation and matrix manipulation.&lt;br&gt;
&amp;gt; &amp;gt; So I would like to know the following.&lt;br&gt;
&amp;gt; &amp;gt; Without using a loop what is the most elegant (and efficient) way to add a &lt;br&gt;
&amp;gt; &amp;gt; new vector to each col of another vector.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The name vector is usually used for stuff that has one row or column. For &lt;br&gt;
&amp;gt; stuff with more than one of these, it is called a matrix, not a vector. &lt;br&gt;
&amp;gt; Altough a vector can also be considered a matrix, but normally we call a &lt;br&gt;
&amp;gt; matrix something that has more than one row or column in it. So 'c' here is &lt;br&gt;
&amp;gt; a matrix not a vector.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; For example:&lt;br&gt;
&amp;gt; &amp;gt; a = [1 5 20 35 100]&lt;br&gt;
&amp;gt; &amp;gt; b = 0:4;&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; c = [1 5 20 35 100&lt;br&gt;
&amp;gt; &amp;gt;       2 6 21 36 101&lt;br&gt;
&amp;gt; &amp;gt;       3 7 22 37 102&lt;br&gt;
&amp;gt; &amp;gt;       4 8 23 38 103&lt;br&gt;
&amp;gt; &amp;gt;       5 9 24 39 104]&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; This is probably pretty easy. Help me out if you can.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To add 'a' to bottom of c matrix, one way is to repmat it below c as follows&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [nRow,nCol]=size(c);&lt;br&gt;
&amp;gt; newc=[c;repmat(a',[1 nCol])]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; newc =&lt;br&gt;
&amp;gt;      1     5    20    35   100&lt;br&gt;
&amp;gt;      2     6    21    36   101&lt;br&gt;
&amp;gt;      3     7    22    37   102&lt;br&gt;
&amp;gt;      4     8    23    38   103&lt;br&gt;
&amp;gt;      5     9    24    39   104&lt;br&gt;
&amp;gt;      1     1     1     1     1&lt;br&gt;
&amp;gt;      5     5     5     5     5&lt;br&gt;
&amp;gt;     20    20    20    20    20&lt;br&gt;
&amp;gt;     35    35    35    35    35&lt;br&gt;
&amp;gt;    100   100   100   100   100&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; similary for b&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --Nasser &lt;br&gt;
&amp;gt; &lt;br&gt;
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 &quot;c&quot; that i described above.</description>
    </item>
  </channel>
</rss>

