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:
solving logic , array and condition, matrix

Subject: solving logic , array and condition, matrix

From: Markov Paris

Date: 29 May, 2012 10:25:56

Message: 1 of 2

Hi All,

Hope you are doing well :)

I have code below. I want to create an array with elements p = [5; 10;
10; 15; 15; 20; 20; 25; 25; 30; ]; and I want replace if statement if
(stateCount(n)> 5 && stateCount(n)<= 10) with value from an array 5
and 10. How can I do that. Any tips will gladly welcome

% value comes from a csv file and store in a matrix name value

value =
2.2034000000000
5.9016000000000
24.0833000000000
25.1000000000000
10.3115000000000
15.3934000000000
20.8525000000000
30.7288000000000
21.4918000000000
6.7833000000000
7.3667000000000
16.7541000000000
34.2034000000000

StateCount = value;

stateHit = zeros(length(stateCount),6);
  for n = 1:length(stateCount)
 if (stateCount(n)<= 5)
        stateHit(n,1) = 1;
 else
 if (stateCount(n)> 5 && stateCount(n)<= 10)
            stateHit(n,2) = 1;

else
if (stateCount(n)>10 && stateCount(n)<= 15)
        stateHit(n,3) = 1;
else
if (stateCount(n)> 15 && stateCount(n)<= 20)
        stateHit(n,4) = 1;
else
 if (stateCount(n)> 20 && stateCount(n)<= 25)
    stateHit(n,5) = 1;
else
if (stateCount(n)> 25 && stateCount(n)<= 30)
    stateHit(n,6) = 1;
end
end
end
end
 end
 end
 end

I want if value = (stateCount(n)> 5 && stateCount(n)<= 10) in then
in column 2

           if value = (stateCount(n)> 10 && stateCount(n)<= 15) in
then in column 3 etc

I have change the code like that below but it doesn't work

    % My modified code

value =

2.2034000000000

5.9016000000000

24.0833000000000

25.1000000000000

10.3115000000000

15.3934000000000

20.8525000000000

30.7288000000000

21.4918000000000

6.7833000000000

7.3667000000000

16.7541000000000

34.2034000000000

p = [5; 10; 10; 15; 15; 20; 20; 25; 25; 30; 30; ];

StateCount = value;

stateHit = zeros(length(stateCount),6);
 for n = 1:length(stateCount)
for i=1:size(p,1)
     if (stateCount(n)<= p(i,:) )
        stateHit(n,1) = 1;
 else
end
end
 end

I can't get result in stateHit, i get values in column. Could please
help me to find the problem in the code

Many thanks in advance

Subject: solving logic , array and condition, matrix

From: Roger Stafford

Date: 30 May, 2012 00:37:09

Message: 2 of 2

Markov Paris <markov.matlab@gmail.com> wrote in message <f44f3b2a-9bd4-4afb-8165-c7fc56932ecd@cu1g2000vbb.googlegroups.com>...
> stateHit = zeros(length(stateCount),6);
> for n = 1:length(stateCount)
> if (stateCount(n)<= 5)
> stateHit(n,1) = 1;
> else
> if (stateCount(n)> 5 && stateCount(n)<= 10)
> stateHit(n,2) = 1;
> else
> if (stateCount(n)>10 && stateCount(n)<= 15)
> stateHit(n,3) = 1;
> else
> if (stateCount(n)> 15 && stateCount(n)<= 20)
> stateHit(n,4) = 1;
> else
> if (stateCount(n)> 20 && stateCount(n)<= 25)
> stateHit(n,5) = 1;
> else
> if (stateCount(n)> 25 && stateCount(n)<= 30)
> stateHit(n,6) = 1;
> end
> end
> end
> end
> end
> end
> end
- - - - - - - - - -
  There are other ways of solving your problem that don't require for-loops. However I'll stick with your for-loops for the purpose of this discussion. You do need a different p. Let p be defined as:

 p = [-inf,5,10,15,20,25,30];

Let v be short for 'value' and h be short for 'stateHit'.

 h = zeros(length(v),length(p)-1);
 for n = 1:length(v)
   for k = 1:length(p)-1
     if p(k) < v(n) & v(n) <= p(k+1)
       h(n,k) = 1;
     end
   end
 end

  It is possible to "vectorize" this using the 'histc' function and with a more general p in case you become adventuresome and want to explore other possibilities.

Roger Stafford

Tags for this Thread

No tags are associated with 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