Thread Subject: Vectorization

Subject: Vectorization

From: Daniel

Date: 12 Jun, 2008 13:43:03

Message: 1 of 6

Is it possible to vectorize the following for - if - loop?

x=randn(10,1);
thres=0.1;
y=zeros(10,1);
for i=1:10
    if x(i) > thres
        y(i) = 1;
    end
end

Thanks for help.

Subject: Vectorization

From: Sebastiaan

Date: 12 Jun, 2008 14:01:02

Message: 2 of 6

"Daniel " <d@f.com> wrote in message
<g2r957$lr1$1@fred.mathworks.com>...
> Is it possible to vectorize the following for - if - loop?
>
> x=randn(10,1);
> thres=0.1;
> y=zeros(10,1);
> for i=1:10
> if x(i) > thres
> y(i) = 1;
> end
> end
>
> Thanks for help.
Yes, use logical indexing. In stead of the loop, use:
y(x>thres) = 1;

Greetz,
Sebastiaan

Subject: Vectorization

From: Rick Rosson

Date: 12 Jun, 2008 14:14:43

Message: 3 of 6


Or:

   y = (x>thres);


Rick


"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message
news:g2ra6u$3tf$1@fred.mathworks.com...
> "Daniel " <d@f.com> wrote in message
> <g2r957$lr1$1@fred.mathworks.com>...
>> Is it possible to vectorize the following for - if - loop?
>>
>> x=randn(10,1);
>> thres=0.1;
>> y=zeros(10,1);
>> for i=1:10
>> if x(i) > thres
>> y(i) = 1;
>> end
>> end
>>
>> Thanks for help.
> Yes, use logical indexing. In stead of the loop, use:
> y(x>thres) = 1;
>
> Greetz,
> Sebastiaan
>


Subject: Vectorization

From: Peter Boettcher

Date: 12 Jun, 2008 14:16:17

Message: 4 of 6

"Daniel " <d@f.com> writes:

> Is it possible to vectorize the following for - if - loop?
>
> x=randn(10,1);
> thres=0.1;
> y=zeros(10,1);
> for i=1:10
> if x(i) > thres
> y(i) = 1;
> end
> end

x=randn(10,1);
thres=0.1;
y = x>thres;

If you need y to be non-logical, then:

y = double(x>thres);

-Peter

Subject: Vectorization

From: Cyrock User

Date: 12 Jun, 2008 15:01:03

Message: 5 of 6

"Daniel " <d@f.com> wrote in message <g2r957$lr1
$1@fred.mathworks.com>...
> Is it possible to vectorize the following for - if -
loop?
>
> x=randn(10,1);
> thres=0.1;
> y=zeros(10,1);
> for i=1:10
> if x(i) > thres
> y(i) = 1;
> end
> end
>
> Thanks for help.

Here is another one:

arrayfun(@(t)t>thres,x)


Subject: Vectorization

From: Cyrock User

Date: 12 Jun, 2008 15:03:01

Message: 6 of 6

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyfxri3fim.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Daniel " <d@f.com> writes:
>
> > Is it possible to vectorize the following for - if -
loop?
> >
> > x=randn(10,1);
> > thres=0.1;
> > y=zeros(10,1);
> > for i=1:10
> > if x(i) > thres
> > y(i) = 1;
> > end
> > end
>
> x=randn(10,1);
> thres=0.1;
> y = x>thres;
>
> If you need y to be non-logical, then:
>
> y = double(x>thres);
>
> -Peter


Just for fun, to show the values that are bigger than
thres:

x(x>thres)


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
vectorize for loop if Daniel 12 Jun, 2008 09:45:05
rssFeed for this Thread

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.

Contact us at files@mathworks.com