# Thread: Finding max min values

1. ## Finding max min values

Hi, I need a script that will let me find extreme values based on measurements. The experiment involved a piston moving horizontaly left/right right/left repeatedly. During the experiment, its position was measured, to make it clear it would be something like this:

1st cycle: -40 -36 -30 ... -4 0 3 ... 31 38 42
2nd cycle: -41 -37 -29 ... -3 0 4 ... 32 39 44

and so on...

Those are just some random values to help illustrate the problem. I have over 30,000 values for a number of cycles so it's just impossible to go through all of them. It's supposed to be done in Matlab. Some while/if loops whould do I guess, but once I start nesting them I always get lost. Help appreciated.

2. Try something like this if you want the min and max of each cycle. If you just want the min max of everything, then you can just repeat the operation again I think.

Code:
A = [-40 -36 -30 -4 0 3 31 38 42;-41 -37 -29 -3 0 4 32 39 44]
min_max = [min(A')' max(A')']%get min max for each cycle
Code:
A =

-40   -36   -30    -4     0     3    31    38    42
-41   -37   -29    -3     0     4    32    39    44

min_max =

-40    42
-41    44
Elbarto

3. Thanks for you help but I guess I forgot to mention some important details.
Assume I have a 1x32000 matrix A. It contains all the measured values. I don't know how many cycles were performed, I don't know how many measurements were taken in each cycle either. But I can tell for sure that measurements for each cycle don't have to be "symmetrical" with respect to 0, I mean for example:

1st cycle: -40 -36 -30 -4 0 3 31 38 42
2nd cycle: -41 -37 -29 -18 -3 0 4 32 44

My first concept was something like this:

Code:
i = 3;

while i<=32000;

if A(1,i-2)>A(1,i-1); A(1,i-1)<A(1,i);
m_i_n=A(1,i-1);

end

if A(1,i-2)<A(1,i-1); A(1,i-1)>A(1,i);
m_a_x=A(1,i-1);
end

i=i+1;

end

I guess all I need now is a matrix that will let me "catch" all those intermediate values but I don't know what syntax to use.

4. can you upload a .mat or .txt file with an example of your data by any chance?

5. Originally Posted by disclaimer
Thanks for you help but I guess I forgot to mention some important details.
Assume I have a 1x32000 matrix A. It contains all the measured values. I don't know how many cycles were performed, I don't know how many measurements were taken in each cycle either. But I can tell for sure that measurements for each cycle don't have to be "symmetrical" with respect to 0, I mean for example:

1st cycle: -40 -36 -30 -4 0 3 31 38 42
2nd cycle: -41 -37 -29 -18 -3 0 4 32 44

My first concept was something like this:

Code:
i = 3;

while i<=32000;

if A(1,i-2)>A(1,i-1); A(1,i-1)<A(1,i);
m_i_n=A(1,i-1);

end

if A(1,i-2)<A(1,i-1); A(1,i-1)>A(1,i);
m_a_x=A(1,i-1);
end

i=i+1;

end

I guess all I need now is a matrix that will let me "catch" all those intermediate values but I don't know what syntax to use.
Try something like:

Code:
A0=A(1:length(A)-1);
A1=A(2:length(A));

mxidx=find((A0-A1)>0);
mx=[A(mxidx),A(length(A)];
mi=[A(1),A(mxidx+1)];
CB

6. Originally Posted by CaptainBlack
Try something like:

Code:
A0=A(1:length(A)-1);
A1=A(2:length(A));

mxidx=find((A0-A1)>0);
mx=A(mxidx);
mi=[A(1),A(mxidx+1)];
CB
Well, thank you but it doesn't give me exactly what I'm looking for. It just creates two matrices and puts every single value in either of them.

Cause the truth is that, while executing the script I posted, Matlab assigns a min/max value for each iteration performed. That gives us "false" min/max values I guess.

Originally Posted by elbarto
can you upload a .mat or .txt file with an example of your data by any chance?
Check the attachment.

Thank you both for your help.

7. Originally Posted by disclaimer
Well, thank you but it doesn't give me exactly what I'm looking for. It just creates two matrices and puts every single value in either of them.
Now that does not agree with my results, even with the bug still in it does not do what you say for me.

Code:
--> A

ans =

Columns 1 to 14
-40 -36 -30  -4   0   3  31  38  42 -40 -36 -30  -4   0
Columns 15 to 27
3  31  38  42 -40 -36 -30  -4   0   3  31  38  42

--> A0=A(1:length(A)-1)

A0 =
Columns 1 to 14
-40 -36 -30  -4   0   3  31  38  42 -40 -36 -30  -4   0
Columns 15 to 26
3  31  38  42 -40 -36 -30  -4   0   3  31  38

--> A1=A(2:length(A)

A1 =
Columns 1 to 14
-36 -30  -4   0   3  31  38  42 -40 -36 -30  -4   0   3
Columns 15 to 26
31  38  42 -40 -36 -30  -4   0   3  31  38  42

--> mxidx=find((A0-A1)>0)

mxidx = 9 18

--> mx=[A(mxidx),A(length(A))]

mx =   42 42 42

--> mi=[A(1),A(mxidx+1)]

mi =   -40 -40 -40

-->
Of course if the values are not increasing through a cycle this will not work.

CB