counting number of times a string appears and graph in histogram

16 次查看(过去 30 天)
str = ["GM100275","Shopping","Sports","Running","Gaming","News","Western Europe";
"AM400497","Movies","Running","Fashion","Yoga","Shopping","Western United States";
"YD300185","Technology","Music","Writing","Writing","Gardening","Western Europe";
"JG400510","Gardening","Writing","Fashion","Technology","Music","Eastern Europe";
"JH100552","Gaming","Technology","Shopping","Fashion","Writing","Western Europe";
"CH300444","Movies","Technology","Astronomy","Cooking","Running","Eastern Europe";
"JA300380","Technology","Astronomy","News","Fashion","Music","Eastern United States";
"VF400625","Astronomy","Music","Running","Writing","Gardening","Canada";
"AJ100593","Reading","Fashion","Technology","Shopping","Writing","Canada";]
NumCanada = count(str,"Canada");
WUS = count(str,"Western United States");
EUS = count(str,"Eastern United States");
CUS = count(str,"Central United States");
NumUS = WUS + EUS + CUS;
A = [NumCanada,NumUS];
subplot(1,5,1)
hist(A)
Here is the main part of my code. The string itself is actually longer but it is in the same format. I am trying to count how many times each locations comes up, Canada, US (western+eastern+central), and three more locations, and put them in a bargraph with 5 different colomns corresponding to 5 locations, and y-axis with the number of people. I am having trouble using "count" functions, because it does not give me a sum, it gives me, for example:
1
0
0
0
1
0
as the string is in this format. Thanks for the help.

回答(1 个)

Rik
Rik 2021-6-9
The count function is doing exactly what you ask it to: you provide it with an array, it returns an array with 1 fewer dimension: a vector.
If you select only the last column, it will work as you expected. Alternatively you can reshape to a vector: str=str(:);.
I would suggest you look into categoricals.
str = ["GM100275","Shopping","Sports","Running","Gaming","News","Western Europe";
"AM400497","Movies","Running","Fashion","Yoga","Shopping","Western United States";
"YD300185","Technology","Music","Writing","Writing","Gardening","Western Europe";
"JG400510","Gardening","Writing","Fashion","Technology","Music","Eastern Europe";
"JH100552","Gaming","Technology","Shopping","Fashion","Writing","Western Europe";
"CH300444","Movies","Technology","Astronomy","Cooking","Running","Eastern Europe";
"JA300380","Technology","Astronomy","News","Fashion","Music","Eastern United States";
"VF400625","Astronomy","Music","Running","Writing","Gardening","Canada";
"AJ100593","Reading","Fashion","Technology","Shopping","Writing","Canada";];
str_location=str(:,end);
NumCanada = count(str_location,"Canada");
WUS = count(str_location,"Western United States");
EUS = count(str_location,"Eastern United States");
CUS = count(str_location,"Central United States");
NumUS = WUS + EUS + CUS;
A = [NumCanada,NumUS];
subplot(1,5,1)
hist(A)

类别

Help CenterFile Exchange 中查找有关 System-Level Simulation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by