Getting the frequency of every 'char' value in one field of struct. Can anyone provide code?

1 回表示 (過去 30 日間)
The index is the ID of the given movie. The movies array has one struct for every movie in the database:
>> movies(1)
ans =
struct with fields:
title: 'Gladiator'
mpaa: ''
release: '05/01/2000'
rating: 8.1000
votecount: 10921
countries: 'GB US' %The countries string contains the two-letter country codes separated by a space (if creddited more than one)
prompt: Calculate the number of movies credited to each country. Return a character array of the two letter country abbreviations and a vector of the corresponding number of movies.
  7 件のコメント
darova
darova 2020 年 4 月 6 日
Maybe something like this?
num = 0;
for i = 1:length(movies)
str = movies(i).countries; % extract string
arr = findstr(str,' ') % find spaces
num = num + sum(arr) + 1;
end
Chiwon Oh
Chiwon Oh 2020 年 4 月 6 日
that code wouldn't know if it already counted a country in previous iterations

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
編集済み: Ameer Hamza 2020 年 4 月 6 日
try this
movies(1).countries = 'GB US';
movies(2).countries = 'GB CA';
movies(3).countries = 'FR';
movies(4).countries = 'US DE';
countries = {movies.countries};
countries = cellfun(@(x) strsplit(x, ' '), countries, 'UniformOutput', 0);
countries = [countries{:}];
summary = cell2table(tabulate(countries), 'VariableNames', {'country', 'number', 'percent'});
Result:
summary =
5×3 table
country number percent
_______ ______ _______
{'GB'} 2 28.571
{'US'} 2 28.571
{'CA'} 1 14.286
{'FR'} 1 14.286
{'DE'} 1 14.286
  6 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
Glad to be of help.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by