Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Applying equation to Matrix

Subject: Applying equation to Matrix

From: Fred O'Con

Date: 22 Aug, 2010 17:20:19

Message: 1 of 4

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

Subject: Applying equation to Matrix

From: Wayne King

Date: 22 Aug, 2010 18:04:06

Message: 2 of 4

"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

Subject: Applying equation to Matrix

From: Roger Stafford

Date: 22 Aug, 2010 20:24:05

Message: 3 of 4

"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

Subject: Applying equation to Matrix

From: Fred O'Con

Date: 22 Aug, 2010 23:16:06

Message: 4 of 4

Thanks very much Roger that is great.

Tags for this Thread

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.

Contact us