|
"Wayne King" <wmkingty@gmail.com> wrote in message <i4roqm$rqs$1@fred.mathworks.com>...
> "Fred O'Con" <barabus12@hotmail.com> wrote in message <i4rm8j$gd7$1@fred.mathworks.com>...
> > I want to apply equation to a matrices elements individualy.
> > ie if any element in C is less than 50 i want it to change element to 0
> > If it is greater than 50 but less than 600 i want it to apply the equation below to the element
> > and less than 1100 and greater than 600 apply the last equation to the element
> >
> > ie working its way through the matrix
> >
> > if 0<C<50;
> > C=0;
> > elseif 600>C>=50;
> > C=(.2666*C+155.29);
> > elseif 1100>C>=600;
> > C=(.4041*C+106.8);
> > else
> > C=C
> > end
>
> Hi Fred, There are many ways to do this. One way is to use logical indexing.
>
> C = randi(1300,20,20);
> C(C<50) = 0;
> % I use > 0 here because you've already set everything less than 50 to zero
> C(C>0 & C<600) = 0.2666*C(C>0 & C<600)+155.29;
>
> And so on.
>
> Wayne
- - - - - - - - - -
Care must be taken when applying such vectorized changes sequentially. Notice that the C numbers lying in [600,1100) are changed into numbers within the [50,600) range, so the [50,600)-transformation must occur before the [600,1100)-transformation. Otherwise some values would undergo double changes.
One easy, safe way to avoid such inadvertent changes in general is to say
t1 = C<50; t2 = 50<=C & C<600; t3 = 600<=C & C<1100;
C(t1) = 0;
C(t2) = .2666*C(t2)+155.29;
C(t3) = .4041*C(t3)+106.8;
This is safe because the t-values are unaffected by any of the subsequent changes in C. (We assume here that the t-values are all disjoint.)
Roger Stafford
|