How to create a counter variable in a loop

13 ビュー (過去 30 日間)
Jack Bratton
Jack Bratton 2018 年 3 月 1 日
編集済み: John D'Errico 2018 年 3 月 1 日
I have a row vector where each entry in the vector is a number 1-9. I need to count how many times each number occurs in the vector and put those sums in a column vector. Any ideas how to do this with a loop?

回答 (2 件)

John D'Errico
John D'Errico 2018 年 3 月 1 日
編集済み: John D'Errico 2018 年 3 月 1 日
Why do you need a loop at all? Why not look for tools that will solve your problem. accumarray is one such tool. Actually, I can think of at least three other tools, and where 4 ways exist, I could find more if I thought or looked. Sparse. histcounts. tabulate. Or you could download my consolidator tool from the File Exchange.
doc accumarray
Sigh. I'll concede it may not have been obvious to use accumarray.
V = ceil(rand(1,100)*9);
accumarray(V(:),1)
ans =
13
11
9
10
11
6
18
12
10
However, now it is your turn. Think about why it works. For that, you need to read the help, which in turn, will help you to use it next time when you need it.
Simplest is tabulate, if you have the stats toolbox.
tabulate(V)
Value Count Percent
1 13 13.00%
2 11 11.00%
3 9 9.00%
4 10 10.00%
5 11 11.00%
6 6 6.00%
7 18 18.00%
8 12 12.00%
9 10 10.00%
Interestingly, I found it took just a bit more effort than I expected it should take to find any of the above tools, once I decided to look (instead of knowing which tools would do the job.) That was disappointing to me, as the help should lead you to find something like this.
A useful tool in MATLAB is the lookfor command. If you want to find a list tools that might do something you need to do in MATLAB, use lookfor. So I tried
lookfor occurrences
count - Returns the number of occurrences of a pattern in text.
But count is not directly able to solve your problem, although it could be made to do the job with some effort. And "lookfor count" did no better.
lookfor frequency
did find tabulate as one possible solution. Although surprisingly, it was not obviously a solution from the one-line comment I found.
tabulate - Frequency table.
And without knowing that sparse, accumarray, and histcounts all could do the job, I had to know they could do it in advance. Hmm. Even my own consolidator tool did not make it obvious from the help that it is able to solve this problem.
[VList,VCounts] = consolidator(V(:),[],'count');
[VList,VCounts]
ans =
1 12
2 18
3 15
4 5
5 4
6 7
7 13
8 15
9 11
I would bet it is answered in the MATLAB FAQ though. And a thorough read of the FAQ is always a good idea.
http://matlab.wikia.com/wiki/FAQ
Surprisingly, I did not find it there easily either. But that is something I can try to resolve.

Jim Riggs
Jim Riggs 2018 年 3 月 1 日
編集済み: Jim Riggs 2018 年 3 月 1 日
What you are describing sounds like a histogram. You can use the function "histcounts" to count the number of times a number appears in the vector.
If A is the vector of numbers, then
histcounts(A,9)
gives the number of occurrences of each value 1 through 9.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by