What does each line do?
3 views (last 30 days)
Show older comments
Hi guys, I'm a Matlab beginner and had some questions about a program my professor gave me.
function T = findmax(A,B)
R = randi([A B],1,20);
C = sign([0 diff( R ) 0]);
T = find(C(1:end-1) > 0 & C(2:end) <= 0)
plot( R )
end
He said that this plots a random array of numbers from A to B, finding where the local maximums are, then tells you where they are.
I understand the R= line. That creates a random 1 by 20 matrix of integers from A to B. I also understand the plot® line.
But I do not understand C= and T=. Could you explain to me what each part of those lines do?
Any info would be helpful, I am pretty confused. Thanks!
0 Comments
Answers (1)
Cedric
on 29 Apr 2013
Edited: Cedric
on 29 Apr 2013
If we execute the following in our command window (you'll have random numbers different from mine, and I work with 10 of them instead of 20, so it doesn't take too much room on the forum):
>> A = 5 ; B = 12 ;
>> R = randi([A, B], 1, 10)
R =
7 5 5 11 10 7 12 5 8 8
here, you are able to find local maximums; think about how you are doing it in your head. Then think about how it is related to difference between each consecutive element, and more particularly to the sign of this difference. Then execute:
>> C = sign([0 diff( R ) 0])
C =
0 -1 0 1 -1 -1 1 -1 1 0 0
and see whether you would be able to spot local max. based on this information (about the sign). Once you understand how to proceed, evaluate
>> C(1:end-1) > 0 & C(2:end) <= 0
ans =
0 0 0 1 0 0 1 0 1 0
and observe that these two conditions:
C(1:end-1) > 0
and
C(2:end) <= 0
are probably what you computed in your head, and the outcome of associating them with a logical AND spots locations of local max. Once it is clear for you, look up FIND in MATLAB doc to understand what it does, given that the above vector is its input argument.
2 Comments
Cedric
on 29 Apr 2013
Edited: Cedric
on 29 Apr 2013
The sign function returns an array of the same size as its input, with -1, 0, 1 at locations where the input is respectively negative, null, or positive. When you don't know, try on the simplest case that you can imagine.
>> sign( [-7, 0, 5] )
ans =
-1 0 1
Now do the same with DIFF to understand what it does, and observe that, as it doesn't output the same number of elements as in its input (I let you figure out why), you would be in trouble managing well boundaries if you would just use sign(diff®). The output of sign® has to be extended a little bit for this reason. Now you'll have to play a bit with this material to fully understand, and again, display the output of everything (remove ";") and evaluate internal blocks of expressions that you don't understand, e.g.
>> C(1:end-1)
ans =
0 -1 0 1 -1 -1 1 -1 1 0
>> C(1:end-1) > 0
ans =
0 0 0 1 0 0 1 0 1 0
and the same for C(2:end), so you understand then what the AND operation does. Finally, FIND returns the position of non-zero elements of its argument, which are places where both conditions are true (AND).
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!