11 views (last 30 days)
Show older comments
Ruben Moreno on 12 May 2022
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names
Commented: Ruben Moreno on 12 May 2022
Accepted Answer: Matt J
Hi, i have a collumn of names of investors in a large dataset and im trying to extract the occurance of every investor. A large quanitity have a combination of investors, meaning there are several names in one cell, so they would only be counted once if i were to count them alone. Is there any way to count all names, even in combination with others?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Matt J on 12 May 2022
Edited: Matt J on 12 May 2022
Open in MATLAB Online
Something like this, perhaps?
investors=["Sam";"Joe";"Sam and Joe"]
investors = 3×1 string array
"Sam" "Joe" "Sam and Joe"
contains(investors,"Sam")
ans = 3×1 logical array
1 0 1
8 Comments Show 6 older commentsHide 6 older comments
Show 6 older commentsHide 6 older comments
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154550
⋮
In simple terms yes, but considering the magnitude of my data and the amount of investors, its not possible to simply write down all names. Added picture for visualization of the problem
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154645
because i want to count all investors in every combination in a data of 33,000 observations. in a total of 8132 unique investor cominations. I cant write the name of all of these investors to count how many there are?
Matt J on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154670
Edited: Matt J on 12 May 2022
Open in MATLAB Online
Why isn't my solution applicable? Is it that you don't hav a prior list of all the investors? If not, will multiple investors always be delimited by commas? If so, you can use strsplit:
investors=["Sam";"Joe";"TPG, Sam, Joe";"TPG, Joe"]
investors = 4×1 string array
"Sam" "Joe" "TPG, Sam, Joe" "TPG, Joe"
Names = cellfun(@(x)strsplit(x,{', '}), investors, 'UniformOutput', false);
Names=string(unique(horzcat(Names{:})'))
Names = 3×1 string array
"Joe" "Sam" "TPG"
count=nan(size(Names));
for i=1:numel(Names)
count(i)=sum(contains(investors,Names{i}));
end
table(Names,count)
ans = 3×2 table
Names count _____ _____ "Joe" 3 "Sam" 2 "TPG" 2
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154715
thank you, will look into this, what does the UniformOutput do in this case?
Matt J on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154725
You're welcome, but if it does what you need, please Accept-click the answer.
UniformOutput=false forces the result of cellfun to be returnd as a cell array,
https://www.mathworks.com/help/matlab/ref/cellfun.html#d123e172702
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154740
i get an error on the first line, Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
Matt J on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154765
The error is not coming from code that you've shown us (or that I've shown you).
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154790
Nvm, i fixed it. It worked now, Wow. thank you very much!
Sign in to comment.
More Answers (1)
Stephen23 on 12 May 2022
Edited: Stephen23 on 12 May 2022
Open in MATLAB Online
str = ["Sam";"Joe";"TPG, Sam, Joe";"TPG, Joe"]
str = 4×1 string array
"Sam" "Joe" "TPG, Sam, Joe" "TPG, Joe"
spl = regexp(str,',','split');
[uni,~,idx] = unique(strtrim([spl{:}]));
cnt = histcounts(idx);
[uni;cnt]
ans = 2×3 string array
"Joe" "Sam" "TPG" "3" "2" "2"
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Ruben Moreno on 12 May 2022
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1717685-counting-outcomes-of-names#comment_2154900
i figured the problem out. But i have a follow up question, I want to use the information i gained. Count of every investors frequency. to add a indication for experience in the original dataset. Creating a dummy for investors observations > 100 and 0 for the rest.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations
Find more on Annotations 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office