"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;
"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
>
> 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
"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.
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:
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.