Search code examples
matlabmatlab-figure

Find two maximum points


I have a two arrays that I plot: A a 1x101 vector and B the same

A = [0.140673450903833  0.143148937279028   0.145430952171596   0.147474938627147   0.149581060870114   0.151187105347571   0.152646348246015   0.153892222566265   0.154913060187075   0.155701930397674   0.156253328260122   0.156562551841967   0.156625533585493   0.156438787610539   0.155999394209637   0.155304997895017   0.154353810555534   0.153144616301392   0.151676776486360   0.149950234280632   0.147965519042205   0.145723755995511   0.143229676241241   0.140476287800831   0.137475884805212   0.134228713435530   0.130738812449387   0.127010778531129   0.123049766057659   0.118861478234099   0.114452155847321   0.109828564345449   0.104997979409803   0.0999681710919947  0.0947473865690700  0.0893443315667412  0.0837681505026921  0.0780284054055798  0.0721350536699391  0.0660984247128685  0.0599291956061627  0.0536383657701255  0.0472372308396036  0.0407373558685518  0.0341505481893574  0.0274888307202605  0.0207644183921863  0.0139897104812690  0.00717740846258673 0.000358034181698980    0.00651349557333709 0.0133637955715171  0.0202018404602034  0.0270147225489001  0.0337898204971252  0.0405146415132138  0.0471768260462406  0.0537641715916784  0.0602646603043279  0.0666664873507057  0.0729580891146200  0.0791281709097673  0.0851657340195109  0.0910601019446384  0.0968009457657087  0.102378308539557   0.107782628657363   0.113004762097380   0.118036003510261   0.122868106079509   0.127493300104313   0.131904310257409   0.136094371477732   0.140057243469708   0.143787223810476   0.147279159770258   0.150528459504324   0.153531108836772   0.156280444813554   0.158783035106175   0.161027296288627   0.163014562505352   0.164743731117677   0.166214276765471   0.167426257040343   0.168380310331524   0.169077651806683   0.169520068571722   0.169709914896378   0.169650109087113   0.169344135453180   0.168796059816963   0.168010582212876   0.166993205517562   0.165750858213848   0.164295206012858   0.162692813100379   0.160590402150861   0.158550181408264   0.156271984944015   0.153800366335689]

B = [-2 -1.96000000000000   -1.92000000000000   -1.88000000000000   -1.84000000000000   -1.80000000000000   -1.76000000000000   -1.72000000000000   -1.68000000000000   -1.64000000000000   -1.60000000000000   -1.56000000000000   -1.52000000000000   -1.48000000000000   -1.44000000000000   -1.40000000000000   -1.36000000000000   -1.32000000000000   -1.28000000000000   -1.24000000000000   -1.20000000000000   -1.16000000000000   -1.12000000000000   -1.08000000000000   -1.04000000000000   -1  -0.960000000000000  -0.920000000000000  -0.880000000000000  -0.840000000000000  -0.800000000000000  -0.760000000000000  -0.720000000000000  -0.680000000000000  -0.640000000000000  -0.600000000000000  -0.560000000000000  -0.520000000000000  -0.480000000000000  -0.440000000000000  -0.400000000000000  -0.360000000000000  -0.320000000000000  -0.280000000000000  -0.240000000000000  -0.200000000000000  -0.160000000000000  -0.120000000000000  -0.0800000000000001 -0.0400000000000000 0   0.0400000000000000  0.0800000000000001  0.120000000000000   0.160000000000000   0.200000000000000   0.240000000000000   0.280000000000000   0.320000000000000   0.360000000000000   0.400000000000000   0.440000000000000   0.480000000000000   0.520000000000000   0.560000000000000   0.600000000000000   0.640000000000000   0.680000000000000   0.720000000000000   0.760000000000000   0.800000000000000   0.840000000000000   0.880000000000000   0.920000000000000   0.960000000000000   1   1.04000000000000    1.08000000000000    1.12000000000000    1.16000000000000    1.20000000000000    1.24000000000000    1.28000000000000    1.32000000000000    1.36000000000000    1.40000000000000    1.44000000000000    1.48000000000000    1.52000000000000    1.56000000000000    1.60000000000000    1.64000000000000    1.68000000000000    1.72000000000000    1.76000000000000    1.80000000000000    1.84000000000000    1.88000000000000    1.92000000000000    1.96000000000000    2];

Plotting these two plot(B,A) I get this enter image description here with two maximum points at B = -1.52 and B = +1.52

I want to add automatically a point as marker in the two maximum values, a horizontal line above the highest point and a two way row pointing from the line to the second peak like this

enter image description here

I tried to sort A and find the position of the two maximum

[val ind] = sort(A,'descend');
max_values = val(1:2)
index = ind(1:2)
r_max = A(ind(1:2))

but the second peak is not the the second position of val because I get this sort:

  Columns 1 through 13

    0.1697    0.1697    0.1695    0.1693    0.1691    0.1688    0.1684    0.1680    0.1674    0.1670    0.1662    0.1658    0.1647

  Columns 14 through 26

    0.1643    0.1630    0.1627    0.1610    0.1606    0.1588    0.1586    0.1566    0.1566    0.1564    0.1563    0.1563    0.1563

The first value 0.1697 (in this case) is the correct one, but the second peak is not in the second position but at the 22nd position.

Looking at the plot, how can I get easily the two maximum points? Once I know the two coordinates I can easily add all the objects that I need.


Solution

  • Using findpeaks (requires Signal Processing Toolbox), yline (introduced in R2018b) and annotation :

    A = [0.140673450903833  0.143148937279028   0.145430952171596   0.147474938627147   0.149581060870114   0.151187105347571   0.152646348246015   0.153892222566265   0.154913060187075   0.155701930397674   0.156253328260122   0.156562551841967   0.156625533585493   0.156438787610539   0.155999394209637   0.155304997895017   0.154353810555534   0.153144616301392   0.151676776486360   0.149950234280632   0.147965519042205   0.145723755995511   0.143229676241241   0.140476287800831   0.137475884805212   0.134228713435530   0.130738812449387   0.127010778531129   0.123049766057659   0.118861478234099   0.114452155847321   0.109828564345449   0.104997979409803   0.0999681710919947  0.0947473865690700  0.0893443315667412  0.0837681505026921  0.0780284054055798  0.0721350536699391  0.0660984247128685  0.0599291956061627  0.0536383657701255  0.0472372308396036  0.0407373558685518  0.0341505481893574  0.0274888307202605  0.0207644183921863  0.0139897104812690  0.00717740846258673 0.000358034181698980    0.00651349557333709 0.0133637955715171  0.0202018404602034  0.0270147225489001  0.0337898204971252  0.0405146415132138  0.0471768260462406  0.0537641715916784  0.0602646603043279  0.0666664873507057  0.0729580891146200  0.0791281709097673  0.0851657340195109  0.0910601019446384  0.0968009457657087  0.102378308539557   0.107782628657363   0.113004762097380   0.118036003510261   0.122868106079509   0.127493300104313   0.131904310257409   0.136094371477732   0.140057243469708   0.143787223810476   0.147279159770258   0.150528459504324   0.153531108836772   0.156280444813554   0.158783035106175   0.161027296288627   0.163014562505352   0.164743731117677   0.166214276765471   0.167426257040343   0.168380310331524   0.169077651806683   0.169520068571722   0.169709914896378   0.169650109087113   0.169344135453180   0.168796059816963   0.168010582212876   0.166993205517562   0.165750858213848   0.164295206012858   0.162692813100379   0.160590402150861   0.158550181408264   0.156271984944015   0.153800366335689];    
    B = [-2 -1.96000000000000   -1.92000000000000   -1.88000000000000   -1.84000000000000   -1.80000000000000   -1.76000000000000   -1.72000000000000   -1.68000000000000   -1.64000000000000   -1.60000000000000   -1.56000000000000   -1.52000000000000   -1.48000000000000   -1.44000000000000   -1.40000000000000   -1.36000000000000   -1.32000000000000   -1.28000000000000   -1.24000000000000   -1.20000000000000   -1.16000000000000   -1.12000000000000   -1.08000000000000   -1.04000000000000   -1  -0.960000000000000  -0.920000000000000  -0.880000000000000  -0.840000000000000  -0.800000000000000  -0.760000000000000  -0.720000000000000  -0.680000000000000  -0.640000000000000  -0.600000000000000  -0.560000000000000  -0.520000000000000  -0.480000000000000  -0.440000000000000  -0.400000000000000  -0.360000000000000  -0.320000000000000  -0.280000000000000  -0.240000000000000  -0.200000000000000  -0.160000000000000  -0.120000000000000  -0.0800000000000001 -0.0400000000000000 0   0.0400000000000000  0.0800000000000001  0.120000000000000   0.160000000000000   0.200000000000000   0.240000000000000   0.280000000000000   0.320000000000000   0.360000000000000   0.400000000000000   0.440000000000000   0.480000000000000   0.520000000000000   0.560000000000000   0.600000000000000   0.640000000000000   0.680000000000000   0.720000000000000   0.760000000000000   0.800000000000000   0.840000000000000   0.880000000000000   0.920000000000000   0.960000000000000   1   1.04000000000000    1.08000000000000    1.12000000000000    1.16000000000000    1.20000000000000    1.24000000000000    1.28000000000000    1.32000000000000    1.36000000000000    1.40000000000000    1.44000000000000    1.48000000000000    1.52000000000000    1.56000000000000    1.60000000000000    1.64000000000000    1.68000000000000    1.72000000000000    1.76000000000000    1.80000000000000    1.84000000000000    1.88000000000000    1.92000000000000    1.96000000000000    2];
    plot(B,A)
    
    % Find peaks.
    [maxValuesY,isMaxY]=findpeaks(A);
    maxValuesX = B(isMaxY);
    
    % Plot horizontal line.
    yline(maxValuesY(2));
    
    % Create arrow.
    ar = annotation('arrow');
    ar.Parent = gca;
    ar.X = [maxValuesX(1), maxValuesX(1)];
    ar.Y = [maxValuesY(2), maxValuesY(1)];
    ar.Color = 'black';
    ar.HeadLength = 3;
    

    enter image description here

    Thanks to marsei for tip on the position of annotation.