0

我正在编写比较向量数据的代码。它应该计算有多少位置(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)
4

1 回答 1

0

如果您以矢量化方式执行此操作,它是否有效?不完全确定您希望程序输出什么。

% locate repeated elements
eq_els = ac(diff(ac) == 0);
% count number of repeated elements (unique)
unique_els = unique(eq_els);
num_equal_els = numel(unique_els);

% create variable-length lists for each unique element
each_eq_list = cell(num_equal_els,1);
for (k = 1:num_equal_els)
  % each vector in the cell array is equal to the elements of ac that are equal to the current unique item
  each_eq_list{[k]} = ac(ac == unique_els(k));
end

的长度each_eq_list{[k]}是重复值的连续重复值的总数的长度k

于 2013-11-19T06:02:34.680 回答