How to count number from database

4 ビュー (過去 30 日間)
John C
John C 2021 年 11 月 30 日
回答済み: dpb 2021 年 11 月 30 日
I have a database which is a 1x11 struct.
I want to find out the total number present of each countries.
My code is
function [country, count] = movies_by_countries4(TV)
List = arrayfun(@(x) x.country, TV, 'UniformOutput', false);
[Group, country] = findgroups(List');
count = accumarray(Group,1);
end
And it produces result
However, I want to calculate the result for each individual countries.
So the expected result is:
country =
'CA'
'CN'
'US'
count =
1
5
6
what could I do to seperate the last struct and divide it into two individual country.

採用された回答

dpb
dpb 2021 年 11 月 30 日
I don't like struct stuff so didn't try to construct it since can't copy an image...but following should provide enough bread crumbs along the trail to be able to get there...
country=[repmat({'US'},5,1);repmat({'CN'},4,1);{'CA'};{'US''CN'}]; % generate the data
ix=contains(country,"'"); % identify the dual identity
c=arrayfun(@(c)split(c,"'"),country(ix),'UniformOutput',false); % and split them apart
country=[country(~ix);c{:}]; % add them to the unique
Above is updated vector; if need the original use different variable name for LHS.
Now get the summary counts...
>> summary(categorical(country))
CA 1
CN 5
US 6
>>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by