How can I count the number of occurences of elements within vectors from randsample?
Show older comments
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 Comment
Jan
on 29 Nov 2012
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]?
Accepted Answer
More Answers (1)
Image Analyst
on 29 Nov 2012
1 vote
Why don't you just get the histogram with hist() or histc()?
6 Comments
Onja Razafindratsima
on 29 Nov 2012
Image Analyst
on 29 Nov 2012
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
on 29 Nov 2012
@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
on 29 Nov 2012
Edited: Onja Razafindratsima
on 29 Nov 2012
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
on 29 Nov 2012
Categories
Find more on Resampling Techniques 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!