# How to make a list of appearance

2 ビュー (過去 30 日間)
R G 2020 年 9 月 25 日
コメント済み: Ameer Hamza 2020 年 9 月 25 日
Hi guys,
I am having some lists of animal which some animal appear many time in many list. So I want to count the appearance of animal and show how many times it appear, then save it to excel file
the data after saving in excel shoulbe be like this
name , count
a 1
b 3
c 2
I have think about 2d- char array but it seems Matlab does not support it.

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

### 採用された回答

Ameer Hamza 2020 年 9 月 25 日

Use findgrpups() and histcounts(). Something like this
names = ["cat" "dog" "cat" "cat" "dog"];
[n, unique_names] = findgroups(names);
freq = accumarray(n(:), 1);
T = table(unique_names(:), freq(:));
Result
>> T
T =
2×2 table
Var1 Var2
_____ ____
"cat" 3
"dog" 2
Write to excel file
writetable(T, 'filename.xlsx')
In R2019a and later, following will also work
names = ["cat" "dog" "cat" "cat" "dog"];
[freq, unique_names] = groupcounts(names(:));
T = table(unique_names(:), freq(:));

#### 6 件のコメント

Ameer Hamza 2020 年 9 月 25 日
Ah! I didn't try to run the code after changing. Try the updated code now. I have now used another function.
R G 2020 年 9 月 25 日
Thank you, it works well now!!!
Ameer Hamza 2020 年 9 月 25 日
I am glad to be of help!

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

### その他の回答 (2 件)

KSSV 2020 年 9 月 25 日
Let str be your list of animals.
xbins = unique(str);
[counts,centres] = hist(str,xbins);
R = [centres counts']

#### 0 件のコメント

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

Steven Lord 2020 年 9 月 25 日
histcounts and histogram can handle categorical arrays.
names = ["cat" "dog" "cat" "cat" "dog"];
animals = categorical(names);
[counts, types] = histcounts(animals);
results = table(types.', counts.', 'VariableNames', ["animal", "population"])

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

### Community Treasure Hunt

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

Start Hunting!

Translated by