How to count similar elements and store this value separately?

2 ビュー (過去 30 日間)
Graduate Student
Graduate Student 2018 年 1 月 29 日
コメント済み: Graduate Student 2018 年 1 月 29 日
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

採用された回答

Image Analyst
Image Analyst 2018 年 1 月 29 日
But 2 shows up 4 times, not 5. Why don't you simply use histcounts:
data = [0,0,0,0,0,0,0,0,1,1,1,1,1,0,2,2,2,2,0,3,3,3,3,3,3,3,0,4,4,4]
[counts, edges] = histcounts(data)
result = [(1:length(counts)-1)', counts(2:end)']
You'll see
result =
1 5
2 4
3 7
4 3
  3 件のコメント
Image Analyst
Image Analyst 2018 年 1 月 29 日
Attaching your data in advance is always a good idea. Prevents wasting time. But at least better late than never. So, this works:
s = load('sweep_ind.mat')
sweep_ind = s.sweep_ind
edges = .5 : 1 : max(sweep_ind)+ 0.5;
[counts, edges] = histcounts(sweep_ind, edges)
result = [(1:length(counts))', counts']
Graduate Student
Graduate Student 2018 年 1 月 29 日
Thanks! This works!

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2018 年 1 月 29 日
A = '000000001111102222033333330444' ;
num = 2 ;
iwant = [length(strfind(A,num2str(num))),num]
  1 件のコメント
Graduate Student
Graduate Student 2018 年 1 月 29 日
Is there something similar to strfind for columns? I ran the code and the error I received was: Input string must have one row.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by