<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088</link>
    <title>MATLAB Central Newsreader - Newbie: Subtract a vector from m-n-p array?</title>
    <description>Feed for thread: Newbie: Subtract a vector from m-n-p array?</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>Tue, 19 Feb 2008 15:37:06 -0500</pubDate>
      <title>Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416056</link>
      <author>Volker K</author>
      <description>Hi all!&lt;br&gt;
&lt;br&gt;
I have a m-n-p array A and a q-1 vector V.&lt;br&gt;
&lt;br&gt;
What I would like to do:&lt;br&gt;
&lt;br&gt;
A(:,:,1)-V(1)&lt;br&gt;
A(:,:,2)-V(2)&lt;br&gt;
A(:,:,3)-V(3)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Anyone has an idea how to do that without a loop?&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
Volker</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 16:10:37 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416073</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fpet32$rj1$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Volker K &amp;lt;klinkv.NOSPAM@yahoo.de&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;I have a m-n-p array A and a q-1 vector V.&lt;br&gt;
&lt;br&gt;
&amp;gt;What I would like to do:&lt;br&gt;
&lt;br&gt;
&amp;gt;A(:,:,1)-V(1)&lt;br&gt;
&amp;gt;A(:,:,2)-V(2)&lt;br&gt;
&amp;gt;A(:,:,3)-V(3)&lt;br&gt;
&lt;br&gt;
&amp;gt;Anyone has an idea how to do that without a loop?&lt;br&gt;
&lt;br&gt;
If we are to assume that you -only- want to do the first three&lt;br&gt;
elements of the vector, then&lt;br&gt;
&lt;br&gt;
A(:,:,1:3) - repmat(V(1:3),size(A,1),size(A,2),1)&lt;br&gt;
&lt;br&gt;
If you meant to do the whole vector V, then there is a problem:&lt;br&gt;
you indicated that the third dimension of A is of length p,&lt;br&gt;
but that the vector V is of length q-1, and unless it happens&lt;br&gt;
that p = q-1 then you either have two many or two few elements of V&lt;br&gt;
to generalize this without further clarification from you.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;Beware of bugs in the above code; I have only proved it correct,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;not tried it.&quot;                                    -- Donald Knuth</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 16:46:22 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416088</link>
      <author>Volker K</author>
      <description>Well, I need to subtract each element of the q-by-1 vector V&lt;br&gt;
once from the m-by-n matrix M.&lt;br&gt;
&lt;br&gt;
I thought I could create a m-by-n-by-q array A and then&lt;br&gt;
subtract each element of V. I cannot create m-n from V with&lt;br&gt;
repmat because the arrays are getting too big and ill run&lt;br&gt;
out of memory. (q is about 10000, m and n are 101).&lt;br&gt;
&lt;br&gt;
So here's the example again:&lt;br&gt;
&lt;br&gt;
M(:,:)-V(1)&lt;br&gt;
M(:,:)-V(2)&lt;br&gt;
M(:,:)-V(3)&lt;br&gt;
M(:,:)-V(4)&lt;br&gt;
...&lt;br&gt;
M(:,:)-V(end)&lt;br&gt;
&lt;br&gt;
But thanks so far!&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in&lt;br&gt;
message &amp;lt;fpev1t$4nf$1@canopus.cc.umanitoba.ca&amp;gt;...&lt;br&gt;
&amp;gt; In article &amp;lt;fpet32$rj1$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt; Volker K &amp;lt;klinkv.NOSPAM@yahoo.de&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;I have a m-n-p array A and a q-1 vector V.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;What I would like to do:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;A(:,:,1)-V(1)&lt;br&gt;
&amp;gt; &amp;gt;A(:,:,2)-V(2)&lt;br&gt;
&amp;gt; &amp;gt;A(:,:,3)-V(3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;Anyone has an idea how to do that without a loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If we are to assume that you -only- want to do the first three&lt;br&gt;
&amp;gt; elements of the vector, then&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A(:,:,1:3) - repmat(V(1:3),size(A,1),size(A,2),1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you meant to do the whole vector V, then there is a&lt;br&gt;
problem:&lt;br&gt;
&amp;gt; you indicated that the third dimension of A is of length p,&lt;br&gt;
&amp;gt; but that the vector V is of length q-1, and unless it happens&lt;br&gt;
&amp;gt; that p = q-1 then you either have two many or two few&lt;br&gt;
elements of V&lt;br&gt;
&amp;gt; to generalize this without further clarification from you.&lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt;    &quot;Beware of bugs in the above code; I have only proved&lt;br&gt;
it correct,&lt;br&gt;
&amp;gt;    not tried it.&quot;                                    --&lt;br&gt;
Donald Knuth</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 18:24:46 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416122</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;fpet32$rj1$1@fred.mathworks.com&amp;gt;, klinkv.NOSPAM@yahoo.de &lt;br&gt;
says...&lt;br&gt;
&amp;gt; Hi all!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a m-n-p array A and a q-1 vector V.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What I would like to do:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A(:,:,1)-V(1)&lt;br&gt;
&amp;gt; A(:,:,2)-V(2)&lt;br&gt;
&amp;gt; A(:,:,3)-V(3)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyone has an idea how to do that without a loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&amp;gt; Volker&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Look at the function bsxfun introduced , I think, in version MATLAB 7.4, &lt;br&gt;
R2007a.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren/&quot;&gt;http://blogs.mathworks.com/loren/&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 19:07:15 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416142</link>
      <author>Doug Schwarz</author>
      <description>In article &amp;lt;fpf14u$1a3$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;nbsp;&quot;Volker K&quot; &amp;lt;klinkv.NOSPAM@yahoo.de&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Well, I need to subtract each element of the q-by-1 vector V&lt;br&gt;
&amp;gt; once from the m-by-n matrix M.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I thought I could create a m-by-n-by-q array A and then&lt;br&gt;
&amp;gt; subtract each element of V. I cannot create m-n from V with&lt;br&gt;
&amp;gt; repmat because the arrays are getting too big and ill run&lt;br&gt;
&amp;gt; out of memory. (q is about 10000, m and n are 101).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So here's the example again:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M(:,:)-V(1)&lt;br&gt;
&amp;gt; M(:,:)-V(2)&lt;br&gt;
&amp;gt; M(:,:)-V(3)&lt;br&gt;
&amp;gt; M(:,:)-V(4)&lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt; M(:,:)-V(end)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But thanks so far!&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
bsxfun(@minus,M,permute(V,[3 2 1]))&lt;br&gt;
&lt;br&gt;
But you still have to have enough memory for the result.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Tue, 19 Feb 2008 23:19:02 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416196</link>
      <author>Volker K</author>
      <description>Problem: I only have Matlab 7.2 :-(&lt;br&gt;
&lt;br&gt;
I wonder that there's&lt;br&gt;
&amp;nbsp;no possibility in MatLab to do these kind of operations:&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(1)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(2)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(3)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(4)&lt;br&gt;
&amp;gt; &amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(end) &lt;br&gt;
&lt;br&gt;
I guess it is a common problem...&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message&lt;br&gt;
&amp;lt;see-CB6AA1.14071719022008@71-129-133-66.dollamir.com&amp;gt;...&lt;br&gt;
&amp;gt; In article &amp;lt;fpf14u$1a3$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt;  &quot;Volker K&quot; &amp;lt;klinkv.NOSPAM@yahoo.de&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Well, I need to subtract each element of the q-by-1 vector V&lt;br&gt;
&amp;gt; &amp;gt; once from the m-by-n matrix M.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I thought I could create a m-by-n-by-q array A and then&lt;br&gt;
&amp;gt; &amp;gt; subtract each element of V. I cannot create m-n from V with&lt;br&gt;
&amp;gt; &amp;gt; repmat because the arrays are getting too big and ill run&lt;br&gt;
&amp;gt; &amp;gt; out of memory. (q is about 10000, m and n are 101).&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So here's the example again:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(1)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(2)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(3)&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(4)&lt;br&gt;
&amp;gt; &amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; M(:,:)-V(end)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; But thanks so far!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; bsxfun(@minus,M,permute(V,[3 2 1]))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But you still have to have enough memory for the result.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; Doug Schwarz&lt;br&gt;
&amp;gt; dmschwarz&amp;ieee,org&lt;br&gt;
&amp;gt; Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Wed, 20 Feb 2008 00:03:54 -0500</pubDate>
      <title>Re: Newbie: Subtract a vector from m-n-p array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/164088#416201</link>
      <author>Doug Schwarz</author>
      <description>In article &amp;lt;fpfo56$6tp$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;nbsp;&quot;Volker K&quot; &amp;lt;klinkv.NOSPAM@yahoo.de&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Problem: I only have Matlab 7.2 :-(&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I wonder that there's&lt;br&gt;
&amp;gt;  no possibility in MatLab to do these kind of operations:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M(:,:)-V(1)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M(:,:)-V(2)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M(:,:)-V(3)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M(:,:)-V(4)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M(:,:)-V(end) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I guess it is a common problem...&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
All hope is not lost.  you can download my genop.m from the FEX and use &lt;br&gt;
it instead of bsxfun.  It will be slower, but it will work and no data &lt;br&gt;
will be copied.&lt;br&gt;
&lt;br&gt;
&amp;lt;&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId&lt;/a&gt;&lt;br&gt;
=10333&amp;objectType=FILE&amp;gt;&lt;br&gt;
&lt;br&gt;
(watch out for wrapped URL)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;genop(@minus,M,permute(V,[3 2 1]))&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
  </channel>
</rss>

