pks array going from 2 arguments to 1

1 次查看(过去 30 天)
clear
g1 = 1;
g2 = 0;
k1 = 0.2;
k2 = 0.1;
n1 = 0;
n2 = 0;
nm = 0;
D = 1;
O = -1;
gam = 0.2;
alph = 0;
C2 = (4.*(abs(g2).^2))./(k1.*k2);
w = (-10:0.1:10);
Pa = (-1i.*w) + (k1/2).*(1+C2);
Pb = (-1i.*w) + (gam/2);
Y1 = (-1i.*g1.*sqrt(k1))./(Pa.*Pb + g1.^2);
Y2 = (-g1.*sqrt(k1.*C2).*exp(1i.*alph))./(Pa.*Pb + g1.^2);
Y3 = (Pa.*sqrt(gam))./(Pa.*Pb + g1.^2);
AY1 = abs(Y1).^2;
AY2 = abs(Y2).^2;
AY3 = abs(Y3).^2;
Sbb = 2*pi.*((abs(Y1).^2).*(n1 + 0.5) + (abs(Y2).^2).*(n2 + 0.5) + (abs(Y3).^2).*(nm + 0.5));
[pks,locs] = findpeaks(Sbb);
Diff = abs(w(locs(1))-w(locs(2)));
figure(1)
plot(w,Sbb)
Running the code above generates a plot with 2 peaks. But as g2 increases, the peaks slowly vanishes/merges into one peak. I intend to find the peak separation as a function of g2 and this is shown in my Diff that I defined.
My issue is of the following: As g2 increases and eventually turns from two peaks into one, Diff cannot be computed since there is only one peak. So I get an error saying Index exceeds matrix dimensions. How can I resolve this by just letting the Diff = 0 the moment it has turn into one peak? (since there are no longer two peaks and so the peak separation should be 0)
I hope my question is clear and I would appreciate any help. Thank you in advance.

采纳的回答

Kevin Xia
Kevin Xia 2017-8-7
Try the following:
[pks,locs] = findpeaks(Sbb);
if numel(pks)==2
Diff = abs(w(locs(1))-w(locs(2)));
elseif numel(pks)==1
Diff=0;
end
figure(1)
plot(w,Sbb)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by