Path: news.mathworks.com!newsfeed-00.mathworks.com!NNTP.WPI.EDU!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: Newbie: Subtract a vector from m-n-p array?
Date: Tue, 19 Feb 2008 16:10:37 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 26
Message-ID: <fpev1t$4nf$1@canopus.cc.umanitoba.ca>
References: <fpet32$rj1$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1203437437 4847 192.70.172.160 (19 Feb 2008 16:10:37 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Tue, 19 Feb 2008 16:10:37 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:452408



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