[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: another vectorization challenge
From: |
Tim Rueth |
Subject: |
RE: another vectorization challenge |
Date: |
Fri, 27 Aug 2010 11:39:10 -0700 |
> -----Original Message-----
> From: Jaroslav Hajek [mailto:address@hidden
> Sent: Friday, August 27, 2010 1:41 AM
> To: address@hidden
> Cc: address@hidden
> Subject: Re: another vectorization challenge
>
> On Fri, Aug 27, 2010 at 3:36 AM, Tim Rueth <address@hidden> wrote:
> > For those of you who like to solve vectorization problems,
> I'm stuck
> > on the
> > following:
> >
> > Let's say I have a vector with values that range from -10 to 10:
> >
> > cpg_v = 20*rand(1,n) - 10;
> >
> > Now, I want to create another vector cr_v of the same size with the
> > following logic:
> >
> > Whenever cpg_v drops below "cr_thd," then cr_v should get a
> 1. Okay,
> > that's
> > easy:
> >
> > cr_v = zeros(1,length(cpg));
> > cr_v (cpg_v < cr_thd) = 1;
> >
> > I also want to know when cpg_v goes back above "rec_thd."
> That's easy, too:
> >
> > rec_v (cpg_v > rec_thd) = 1;
> >
> > But here's the tricky part: Reading cpg_v from 1:end, once
> a "1" is
> > encountered at the corresponding location in cr_v, then
> cr_v should be
> > assigned a "2" after that location (unless cpg_v drops below cr_thd
> > again) until the point where cpg_v is greater than "rec_thd." Note
> > that a "2" shouldn't be assigned until there's a 1 in cr_v, and the
> > run of 2's should stop once a 1 is encountered in rec_v. Here's a
> > simple example (with cr_thd = -4, rec_thd = 4), and the
> desired output:
> >
>
> I usually like to help, but I don't understand your tricky
> part description. If I did, 4th column should end with 2's.
>
> > cpg_v cr_v rec_v desired cr_v
> > -------------------------------------------------------------------
> > 6.45824 0.00000 1.00000 0.00000
> > 7.42197 0.00000 1.00000 0.00000
> > -2.41237 0.00000 0.00000 0.00000
> > 4.85508 0.00000 1.00000 0.00000
> > 3.78177 0.00000 0.00000 0.00000
> > -3.24793 0.00000 0.00000 0.00000
> > -5.63435 1.00000 0.00000 1.00000
> > -2.10278 0.00000 0.00000 2.00000
> > -0.80376 0.00000 0.00000 2.00000
> > 7.97556 0.00000 1.00000 0.00000
> > -5.11177 1.00000 0.00000 1.00000
> > -4.56460 1.00000 0.00000 1.00000
> > -8.43245 1.00000 0.00000 1.00000
> > 4.85563 0.00000 1.00000 0.00000
> > -2.17610 0.00000 0.00000 0.00000
> > 4.87113 0.00000 1.00000 0.00000
> > 6.78267 0.00000 1.00000 0.00000
> > 9.30517 0.00000 1.00000 0.00000
> > 5.31377 0.00000 1.00000 0.00000
> > -4.93238 1.00000 0.00000 1.00000
> > 2.10757 0.00000 0.00000 2.00000
> > 6.94569 0.00000 1.00000 0.00000
> > 1.67053 0.00000 0.00000 0.00000
> > -7.23665 1.00000 0.00000 1.00000
> > -5.43656 1.00000 0.00000 1.00000
> > -9.79481 1.00000 0.00000 1.00000
> > 0.45726 0.00000 0.00000 2.00000
> > -2.14684 0.00000 0.00000 1.00000
> > -0.75054 0.00000 0.00000 1.00000
> > -2.59986 0.00000 0.00000 1.00000 Obviously, this is
> easy to do
> > in a for-loop, but is there a way to do it just with vectors?
> >
>
> If you can do it with a for loop, why don't you post it here? Not only
> that can clarify your intent, but can also serve as an alternative
> code to check against.
>
> regards
>
> --
> RNDr. Jaroslav Hajek, PhD
> computing expert & GNU Octave developer
> Aeronautical Research and Test Institute (VZLU)
> Prague, Czech Republic
> url: www.highegg.matfyz.cz
Oh, yes, sorry for any confusion. This should make it easier to understand:
Here's a for-loop that I wrote, followed by another test scenario. Even
though I show cr_thd and rec_thd as scalars, they are actually matrices
produced from ndgrid(), so can you not only vectorize this for-loop, but
also ensure that it will work when cr_thd and rec_thd are n-dim arrays?
That would be awesome!
# create a test scenario
n = 30; # number of elements to test
cr_thd = -4; # crash threshold
rec_thd = 4; # recovery threshold
cpg = 20*rand(n,1) - 10;
# initialize the array
cr_v = zeros(n,1);
cr_v(1) = cpg(1) < cr_thd; # specify first element
# loop through the rest of the elements
for i = 2:n
if ( (cr_v(i-1) == 1) | (cr_v(i-1) == 2) ) & (cpg(i) <= rec_thd)
cr_v(i) = 2; # have not recovered yet
endif;
if cpg(i) < cr_thd
cr_v(i) = 1; # crashed again (which can happen again even
if not recovered)
endif;
endfor;
fid = fopen("temp.txt",'wt');
fdisp(fid,sprintf(" cpg_v\tcr_v"));
matrix = [cpg cr_v];
fdisp(fid,matrix);
fclose(fid);
cpg_v cr_v
5.27733 0.00000
-3.72553 0.00000
0.21305 0.00000
5.21520 0.00000
-2.73123 0.00000
-4.15256 1.00000
-9.61998 1.00000
7.03937 0.00000
2.59400 0.00000
4.20354 0.00000
-3.62042 0.00000
5.50072 0.00000
5.77591 0.00000
5.19524 0.00000
1.43837 0.00000
3.60592 0.00000
-6.17330 1.00000
-6.33281 1.00000
0.86042 2.00000
-4.83423 1.00000
-2.84996 2.00000
-2.06245 2.00000
0.64466 2.00000
-0.73008 2.00000
1.53458 2.00000
8.15784 0.00000
-4.50903 1.00000
5.88615 0.00000
5.99068 0.00000
1.15822 0.00000
Thanks,
--Tim