Thread Subject: Newbie: Subtract a vector from m-n-p array?

Subject: Newbie: Subtract a vector from m-n-p array?

From: Volker K

Date: 19 Feb, 2008 15:37:06

Message: 1 of 7

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

Subject: Newbie: Subtract a vector from m-n-p array?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 19 Feb, 2008 16:10:37

Message: 2 of 7

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?

From: Volker K

Date: 19 Feb, 2008 16:46:22

Message: 3 of 7

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!


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?

From: Loren Shure

Date: 19 Feb, 2008 18:24:46

Message: 4 of 7

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.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Newbie: Subtract a vector from m-n-p array?

From: Doug Schwarz

Date: 19 Feb, 2008 19:07:15

Message: 5 of 7

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?

From: Volker K

Date: 19 Feb, 2008 23:19:02

Message: 6 of 7

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...


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?

From: Doug Schwarz

Date: 20 Feb, 2008 00:03:54

Message: 7 of 7

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.

<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId
=10333&objectType=FILE>

(watch out for wrapped URL)


  genop(@minus,M,permute(V,[3 2 1]))

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com