Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: how to do this without for loops?

Subject: how to do this without for loops?

From: bahoo

Date: 09 Aug, 2007 21:26:21

Message: 1 of 3

Hi,

I have two vectors, v1 and v2, with possibly different number of
elements.
I want to compute a matrix D without using for loops, that stores the
difference:

so that
D(i,j) = v1(i) - v2(j)

Can anyone show me how to do this without for loops?
Thanks!
bahoo

Subject: Re: how to do this without for loops?

From: us

Date: 09 Aug, 2007 21:41:03

Message: 2 of 3

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

Subject: Re: how to do this without for loops?

From: Loren Shure

Date: 10 Aug, 2007 11:35:15

Message: 3 of 3

In article <1186694781.588924.201810@x35g2000prf.googlegroups.com>,
b83503104@yahoo.com says...
> Hi,
>
> I have two vectors, v1 and v2, with possibly different number of
> elements.
> I want to compute a matrix D without using for loops, that stores the
> difference:
>
> so that
> D(i,j) = v1(i) - v2(j)
>
> Can anyone show me how to do this without for loops?
> Thanks!
> bahoo
>
>

I make sure one is a column and the other a row inside the call to
bsxfun.

c1 = bsxfun(@minus,a(:),b(:).')

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

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 09 Aug, 2007 17:45:10
meshgrid us 09 Aug, 2007 17:45:10
arrayfun us 09 Aug, 2007 17:45:10
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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