Path: news.mathworks.com!not-for-mail
From: "us " <us@neurol.unizh.ch>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to do this without for loops?
Date: Thu, 9 Aug 2007 21:41:03 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 21
Message-ID: <f9g1lf$p92$1@fred.mathworks.com>
References: <1186694781.588924.201810@x35g2000prf.googlegroups.com>
Reply-To: "us " <us@neurol.unizh.ch>
NNTP-Posting-Host: webapp-00-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1186695663 25890 172.30.248.35 (9 Aug 2007 21:41:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 9 Aug 2007 21:41:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:423279


bahoo:
<SNIP in search of a mesh

> D(i,j) = v1(i) - v2(j)
> Can anyone show me how to do this without for loops...

two of the many solutions

     a=1:5;
     b=[10,100];
% one
     [x,y]=meshgrid(a,b);
     r1=x-y;
% two
     r2=arrayfun(@(x) x-b,a,'uni',false);
     r2=cat(1,r2{:}).';
% the result
     r1
     isequal(r1,r2)

us