# Count the number of times a word appears?

35 ビュー (過去 30 日間)
Beth Lewis 2021 年 8 月 17 日

So I have a mixed text and numerical document that is located in a table. In column A it describes text values (fish species surveyed), where a specific text can be repeated multiple times along the rows.
In column B, it describes a numerical value of how many times that fish was surveyed in that particular instance. But the fish can be seen again, with different numeric values next to it.
For example:
A: B:
fishX 3
fishY 3
fishZ 2
fishX 2
fishX 2
So what I want to do is count the number of times Fish X, Y, Z has been surveyed overall, which I am finding difficult because they are two different mediums, across mulitple rows. It would require the code being able to go FishX=3+2+2.
This is all to count the most common Fish sighted in a survey.
Hope that makes sense

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

### 回答 (3 件)

Yazan 2021 年 8 月 17 日
A = {'fishX', 'fishY', 'fishZ', 'fishX', 'fishX'}';
B = [3 3 2 2 2]';
T = table(A, B);
groupsummary(T, "A", 'sum')
ans = 3×3 table
A GroupCount sum_B _________ __________ _____ {'fishX'} 3 7 {'fishY'} 1 3 {'fishZ'} 1 2
##### 0 件のコメント表示非表示 -1 件の古いコメント

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

Simon Chan 2021 年 8 月 17 日
clear; clc;
G = groupsummary(A,'A','sum');
A new column is generated which is the result.
G =
3×3 table
A GroupCount sum_B
_________ __________ _____
{'fishX'} 3 7
{'fishY'} 1 3
{'fishZ'} 1 2
##### 0 件のコメント表示非表示 -1 件の古いコメント

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

KSSV 2021 年 8 月 17 日
A = {'fishX' ;
'fishY' ;
'fishZ' ;
'fishX' ;
'fishX' } ;
B = [3 ; 3 ; 2 ; 2 ; 2]
idx = strcmp(A,'fishX') ;
iwant = sum(B(idx))

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

### カテゴリ

Find more on LaTeX 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!

Translated by