Arrays 未定义数组
我仍然很困惑,为什么我不能知道我的数组的这个小算法的结果。该阵列有近1000个数字1-D。我正在尝试查找峰值和每个峰值的索引。我确实找到了峰值,但我找不到它们的指数。你能帮帮我吗。我想绘制所有的值,而不考虑索引Arrays 未定义数组,arrays,matlab,Arrays,Matlab,我仍然很困惑,为什么我不能知道我的数组的这个小算法的结果。该阵列有近1000个数字1-D。我正在尝试查找峰值和每个峰值的索引。我确实找到了峰值,但我找不到它们的指数。你能帮帮我吗。我想绘制所有的值,而不考虑索引 %clear all %close all %clc %// not generally appreciated %----------------------------------- %message1.txt. %---------------------------
%clear all
%close all
%clc
%// not generally appreciated
%-----------------------------------
%message1.txt.
%-----------------------------------
% t=linspace(0,tmax,length(x)); %get all numbers
% t1_n=0:0.05:tmax;
x=load('ww.txt');
tmax= length(x) ;
tt= 0:tmax -1;
x4 = x(1:5:end);
t1_n = 1:5:tt;
x1_n_ref=0;
k=0;
for i=1:length(x4)
if x4(i)>170
if x1_n_ref-x4(i)<0
x1_n_ref=x4(i);
alpha=1;
elseif alpha==1 && x1_n_ref-x4(i)>0
k=k+1;
peak(k)=x1_n_ref; // This is my peak value. but I also want to know the index of it. which will represent the time.
%peak_time(k) = t1_n(i); // this is my issue.
alpha=2;
end
else
x1_n_ref=0;
end
end
%----------------------
figure(1)
% plot(t,x,'k','linewidth',2)
hold on
% subplot(2,1,1)
grid
plot( x4,'b'); % ,tt,x,'k'
legend('down-sampling by 5');
%全部清除
%全部关闭
%clc
%//不被普遍认可
%-----------------------------------
%message1.txt。
%-----------------------------------
%t=linspace(0,tmax,长度(x));%获取所有数字
%t1_n=0:0.05:tmax;
x=加载('ww.txt');
tmax=长度(x);
tt=0:tmax-1;
x4=x(1:5:结束);
t1_n=1:5:tt;
x1_n_ref=0;
k=0;
对于i=1:长度(x4)
如果x4(i)>170
如果x1_n_ref-x4(i)0
k=k+1;
峰值(k)=x1\u n\u ref;//这是我的峰值。但我也想知道它的索引。这将代表时间。
%峰值时间(k)=t1_n(i);//这是我的问题。
α=2;
结束
其他的
x1_n_ref=0;
结束
结束
%----------------------
图(1)
%绘图(t,x,'k','linewidth',2)
等等
%子地块(2,1,1)
网格
绘图(x4,'b');%,tt,x,'k'
图例(“向下采样5”);
以下是您的错误:
tmax= length(x) ;
tt= 0:tmax -1;
x4 = x(1:5:end);
t1_n = 1:5:tt; % <---
以下是您的错误:
tmax= length(x) ;
tt= 0:tmax -1;
x4 = x(1:5:end);
t1_n = 1:5:tt; % <---
您需要正确索引tt数组。 你可以用
t1_n = tt(1:5:end); % note that this will give a zero based index, rather than a 1 based index, due to t1_n starting at 0. you can use t1_n = 1:tmax if you want 1 based (matlab style)
您还可以稍微减少代码,有些变量似乎没有使用,或者可能没有必要使用——包括t1_n变量:
x=load('ww.txt');
tmax= length(x);
x4 = x(1:5:end);
xmin = 170
% now change the code
maxnopeaks = round(tmax/2);
peaks(maxnopeaks)=0; % preallocate the peaks for speed
index(maxnopeaks)=0; % preallocate index for speed
i = 0;
for n = 2 : tmax-1
if x(n) > xmin
if x(n) >= x(n-1) & x(n) >= x(n+1)
i = i+1;
peaks(i) = t(n);
index(i) = n;
end
end
end
% now trim the excess values (if any)
peaks = peaks(1:i);
index = index(1:i);
您需要正确索引tt数组。 你可以用
t1_n = tt(1:5:end); % note that this will give a zero based index, rather than a 1 based index, due to t1_n starting at 0. you can use t1_n = 1:tmax if you want 1 based (matlab style)
您还可以稍微减少代码,有些变量似乎没有使用,或者可能没有必要使用——包括t1_n变量:
x=load('ww.txt');
tmax= length(x);
x4 = x(1:5:end);
xmin = 170
% now change the code
maxnopeaks = round(tmax/2);
peaks(maxnopeaks)=0; % preallocate the peaks for speed
index(maxnopeaks)=0; % preallocate index for speed
i = 0;
for n = 2 : tmax-1
if x(n) > xmin
if x(n) >= x(n-1) & x(n) >= x(n+1)
i = i+1;
peaks(i) = t(n);
index(i) = n;
end
end
end
% now trim the excess values (if any)
peaks = peaks(1:i);
index = index(1:i);