我正在编写比较向量数据的代码。它应该计算有多少位置(acc
)具有相等的值,并将特定值保存在与位置数量(n_T(acc)
)相同长度的向量中。
我的数据向量是 [30000 x 1]。例如,前 80 个位置具有相同的值,接下来的 60 个位置具有相同的值,等等,接下来的 5 个位置具有相同的值。如果我只使用 29996 个值,则代码运行良好。我不明白为什么当我尝试使用完整的向量时,MATLAB 一直很忙。
检查我的数据向量,我注意到最后 5 个位置是等效的 [29996:30000]。可能是原因,我应该改变什么?
以下是代码
%========================================================
%ac: data vector`
%acc1: accumulator which count how much positions have the same value
%n_T: vector which presents the values I need, in the same positions the data is equal
%m: show a value where i should begin
%========================================================
i=m; %previously used`
fv=length(ac)
while i<fv %29996
acc1=0;
for i=m+1:fv
if ac(i)==ac(i-1)
acc1=acc1+1; % count how much positions are equals
else
m=i;
break
end
end
mi=m-acc1; %define where the data n_T should begin
for i=mi:m
n_T(i)=tm/acc1; %create a vector with length [acc x1] begining in mi and finishing in m
end
m=i;
end
plot(n_T)