In article <fpet32$rj1$1@fred.mathworks.com>,
Volker K <klinkv.NOSPAM@yahoo.de> wrote:
>I have a m-n-p array A and a q-1 vector V.
>What I would like to do:
>A(:,:,1)-V(1)
>A(:,:,2)-V(2)
>A(:,:,3)-V(3)
>Anyone has an idea how to do that without a loop?
If we are to assume that you -only- want to do the first three
elements of the vector, then
A(:,:,1:3) - repmat(V(1:3),size(A,1),size(A,2),1)
If you meant to do the whole vector V, then there is a problem:
you indicated that the third dimension of A is of length p,
but that the vector V is of length q-1, and unless it happens
that p = q-1 then you either have two many or two few elements of V
to generalize this without further clarification from you.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth
Subject: Newbie: Subtract a vector from m-n-p array?
Well, I need to subtract each element of the q-by-1 vector V
once from the m-by-n matrix M.
I thought I could create a m-by-n-by-q array A and then
subtract each element of V. I cannot create m-n from V with
repmat because the arrays are getting too big and ill run
out of memory. (q is about 10000, m and n are 101).
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fpev1t$4nf$1@canopus.cc.umanitoba.ca>...
> In article <fpet32$rj1$1@fred.mathworks.com>,
> Volker K <klinkv.NOSPAM@yahoo.de> wrote:
>
> >I have a m-n-p array A and a q-1 vector V.
>
> >What I would like to do:
>
> >A(:,:,1)-V(1)
> >A(:,:,2)-V(2)
> >A(:,:,3)-V(3)
>
> >Anyone has an idea how to do that without a loop?
>
> If we are to assume that you -only- want to do the first three
> elements of the vector, then
>
> A(:,:,1:3) - repmat(V(1:3),size(A,1),size(A,2),1)
>
> If you meant to do the whole vector V, then there is a
problem:
> you indicated that the third dimension of A is of length p,
> but that the vector V is of length q-1, and unless it happens
> that p = q-1 then you either have two many or two few
elements of V
> to generalize this without further clarification from you.
> --
> "Beware of bugs in the above code; I have only proved
it correct,
> not tried it." --
Donald Knuth
Subject: Newbie: Subtract a vector from m-n-p array?
In article <fpet32$rj1$1@fred.mathworks.com>, klinkv.NOSPAM@yahoo.de
says...
> Hi all!
>
> I have a m-n-p array A and a q-1 vector V.
>
> What I would like to do:
>
> A(:,:,1)-V(1)
> A(:,:,2)-V(2)
> A(:,:,3)-V(3)
>
>
> Anyone has an idea how to do that without a loop?
>
> Thanks!
> Volker
>
>
Look at the function bsxfun introduced , I think, in version MATLAB 7.4,
R2007a.
In article <fpf14u$1a3$1@fred.mathworks.com>,
"Volker K" <klinkv.NOSPAM@yahoo.de> wrote:
> Well, I need to subtract each element of the q-by-1 vector V
> once from the m-by-n matrix M.
>
> I thought I could create a m-by-n-by-q array A and then
> subtract each element of V. I cannot create m-n from V with
> repmat because the arrays are getting too big and ill run
> out of memory. (q is about 10000, m and n are 101).
>
> So here's the example again:
>
> M(:,:)-V(1)
> M(:,:)-V(2)
> M(:,:)-V(3)
> M(:,:)-V(4)
> ...
> M(:,:)-V(end)
>
> But thanks so far!
bsxfun(@minus,M,permute(V,[3 2 1]))
But you still have to have enough memory for the result.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
Subject: Newbie: Subtract a vector from m-n-p array?
I wonder that there's
no possibility in MatLab to do these kind of operations:
> > M(:,:)-V(1)
> > M(:,:)-V(2)
> > M(:,:)-V(3)
> > M(:,:)-V(4)
> > ...
> > M(:,:)-V(end)
I guess it is a common problem...
Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-CB6AA1.14071719022008@71-129-133-66.dollamir.com>...
> In article <fpf14u$1a3$1@fred.mathworks.com>,
> "Volker K" <klinkv.NOSPAM@yahoo.de> wrote:
>
> > Well, I need to subtract each element of the q-by-1 vector V
> > once from the m-by-n matrix M.
> >
> > I thought I could create a m-by-n-by-q array A and then
> > subtract each element of V. I cannot create m-n from V with
> > repmat because the arrays are getting too big and ill run
> > out of memory. (q is about 10000, m and n are 101).
> >
> > So here's the example again:
> >
> > M(:,:)-V(1)
> > M(:,:)-V(2)
> > M(:,:)-V(3)
> > M(:,:)-V(4)
> > ...
> > M(:,:)-V(end)
> >
> > But thanks so far!
>
>
> bsxfun(@minus,M,permute(V,[3 2 1]))
>
> But you still have to have enough memory for the result.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.
Subject: Newbie: Subtract a vector from m-n-p array?
In article <fpfo56$6tp$1@fred.mathworks.com>,
"Volker K" <klinkv.NOSPAM@yahoo.de> wrote:
> Problem: I only have Matlab 7.2 :-(
>
> I wonder that there's
> no possibility in MatLab to do these kind of operations:
> > > M(:,:)-V(1)
> > > M(:,:)-V(2)
> > > M(:,:)-V(3)
> > > M(:,:)-V(4)
> > > ...
> > > M(:,:)-V(end)
>
> I guess it is a common problem...
All hope is not lost. you can download my genop.m from the FEX and use
it instead of bsxfun. It will be slower, but it will work and no data
will be copied.
Public Submission Policy
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 Disclaimer prior to use.