0

I am writing code which compares the data of a vector. It should count how many positions (acc) have equal values, and save a specific value in a vector of the same length of the quantity of positions (n_T(acc)).

My data vector is [30000 x 1]. For example, first 80 positions have the same value, next 60 positions have the same value, etc., next 5 positions have the same value. The code works well if I use just 29996 values. I do not understand why when I try to use the complete vector MATLAB stays Busy.

Checking my data vector, I noticed that the last 5 positions are equivalent [29996:30000]. Could it be the reason, and what should I change?

Following is the code

%========================================================
%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)
chappjc
  • 30,359
  • 6
  • 75
  • 132

1 Answers1

0

Does it work if you do this in a vectorized way? Not completely sure what you want the program to output.

% 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

The length of each_eq_list{[k]} is the length of the total number of contiguous repeated values of repeated value k.

bright-star
  • 6,016
  • 6
  • 42
  • 81