找到两个最大点

问题描述 投票:2回答:2

我有一个两个数组,我绘制:A 1x101矢量和B相同

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)我得到这个enter image description here有两个最大点在B = -1.52和B = +1.52

我想在两个最大值中自动添加一个点作为标记,最高点上方的水平线和从该线指向第二个峰值的双向行

enter image description here

我试图对A进行排序并找到两个最大值的位置

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

但第二个峰值不是val的第二个位置,因为我得到了这样的:

  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

第一个值0.1697(在这种情况下)是正确的值,但第二个峰值不在第二个位置,而是在第22个位置。

看看情节,我怎样才能轻松获得两个最高点?一旦我知道了两个坐标,我就可以轻松添加我需要的所有对象。

matlab matlab-figure
2个回答
5
投票

使用findpeaks(需要信号处理工具箱),yline(在R2018b中引入)和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

感谢marsei关于注释位置的提示。


4
投票

如果你特别有这样的图,你可以使用以下解决方案,它只是排除了第一个找到的最大值附近的n邻居。

% Input (copy from above...)
A = [ .. ];
B = [ .. ];

% Index of max value.
[max_val, max_idx] = max(A);

% Find second max value by excluding n neighbourhood.
n = 10;
AA = A;
AA(max_idx - n : max_idx + n) = [];
sec_max_val = max(AA);
sec_max_idx = find(A == sec_max_val);

% Output.
figure(1);
hold on;

% Graph.
plot(B, A);

% Black line.
plot([B(1) B(end)], [max_val max_val], 'k');

% Black arrow.
p1 = [B(sec_max_idx) B(sec_max_idx)];
p2 = [max_val sec_max_val];
dp = p2 - p1;
quiver(p1(1), p2(1), p1(2) - p1(1), p2(2) - p2(1), 0, 'k'); 

hold off;

你会得到这样的输出:

Output

© www.soinside.com 2019 - 2024. All rights reserved.