How to count similar elements and store this value separately?
Show older comments
I have a data set that is one column and looks like:
[0000000011111022222033333330444]
I am trying to count the number of elements for a specific number. The list goes up to 170, increasing by 1 integer. And a 0 breaks up the previous number and the next number. I am attempting to count the occurrence of the numbers. For example, for 1 it would be 5 since shows up 5 times before moving onto 2.
Ideally, I am hoping to store this information separately to look like [5,2], something to show that 2 shows up 5 times.
loop_counter = 0;
m = 0;
y = zeros(length(x))
for i = 1:length(x)
if x(i,1) == 1
loop_counter = loop_counter + 1;
else
y(i, 1) = loop_counter;
m = m + 1;
end
end
Accepted Answer
More Answers (1)
KSSV
on 29 Jan 2018
A = '000000001111102222033333330444' ;
num = 2 ;
iwant = [length(strfind(A,num2str(num))),num]
1 Comment
Graduate Student
on 29 Jan 2018
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!