How can I count the number of occurences of elements within vectors from randsample?
古いコメントを表示
Hi, I am running a randsample of 1000 iterations using for loop, and I want to count the total number of occurences of each elements in the randomized sample. Here is what I did: for a = 1:1:1000 x = randsample(population,5) %5 is just an example, I have more than that; and population is a string data that I loaded using the textread command end
And I get 1000 vectors x with 5 elements from the population (i.e. x = 12345; x=23456; x=24567 etc ...); but what I really want to know is how many of each element are in all these x vectors i.e. for 1 there are in total 200, for 2 there are 300 and so on ...instead of gettin a list of what's in X.
What command should I put before the "end" to get such data? Thanks in advance for your help.
1 件のコメント
Jan
2012 年 11 月 29 日
In x = 12345, tha variable x does not have 5 elements, but only 1. Do you mean x = '12345' or x = [1,2,3,4,5]?
採用された回答
その他の回答 (1 件)
Image Analyst
2012 年 11 月 29 日
1 投票
Why don't you just get the histogram with hist() or histc()?
6 件のコメント
Onja Razafindratsima
2012 年 11 月 29 日
Image Analyst
2012 年 11 月 29 日
I don't see why it would not work. I don't have randsample. Can you give a short example, say about 20 numbers, and show what hist gives and show us what you want so I can see why hist is wrong?
Jan
2012 年 11 月 29 日
@Onja: Counting the number of occurrences is obvioulsy a task for histc(). When you mention, that you have tried it without success, post the code and explain the problems. Then a suggestion for improvements is easy.
Onja Razafindratsima
2012 年 11 月 29 日
編集済み: Onja Razafindratsima
2012 年 11 月 29 日
Onja, stop for a second and think about what you see! Read the error message, then look at this:
help histc
and compare what the help describes as the correct number of input arguments (the subject of the error message!) with what you are passing the function. If you don't take the time to investigate such a simple thing you will not learn how to solve your problem...
Onja Razafindratsima
2012 年 11 月 29 日
カテゴリ
ヘルプ センター および File Exchange で Descriptive Statistics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!