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 Comment

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]?

Sign in to comment.

 Accepted Answer

POP = 'abcdefghijklmnop';
N = 1000;
M = 5;
x = zeros(N,M);
for a = 1:N
x(a,:) = randsample(POP,M); % Each row is a vector sample.
end
c = histc(x(:),POP).';
for ii = 1:length(C),fprintf('%s: %i\n',POP(ii),C(ii)),end
a: 334
b: 324
c: 316
d: 291
e: 282
f: 327
g: 282
h: 319
i: 326
j: 297
k: 302
l: 312
m: 318
n: 353
o: 321
p: 296
Note: if your POP is not sorted, you will get an error. So first, make a new variable called POPs = sort(POP), then use this with HISTC and the counts will correspond to POPs.

7 Comments

this apparently works well, but when I use my actual POP data (which is larger than abcdefghijklmnop), I get the following message:
??? The following error occurred converting from cell to double:
Error using ==> double
Conversion to double from cell is not possible.
I looked up help double but I'm not sure which variable should I convert to double precision. any thought on that? thanks.
Is POP a cell array? You don't say which line throws the error so it is hard to debug it. Recall that I can't see your computer screen or data.....
The error occurs after this:
for a = 1:N
x(a,:) = randsample(POP,M)
end
My data looks something like this:
POP =
'Abrahamia_sp'
'Agauria_polyphylla'
'Albizia_gummifera'
'Allophylus_cobe'
'Anisophyllea_fallax'
'Anthocleista_amplexicaulis'
'Antidesma_petiolare'
'Antirhea_borbonica'
'Aphloia_theaformis'
'Apodytes_dimidiata'
'Aspidostemon_humbertianum'
'Beguea_apetala'
'Beilschmiedia_velutina'
'Brachylaena_ramiflora'
'Breonia_sp'
'Bridelia_tulasneana'
'Burasaia_madagascariensis'
'Cabucala_cryptophlebia'
'Calophyllum_drouhardii'
So you want to do this one at a time. Use POP{1}, etc.
Each one is not a population, but they constitute the elements in the population POP, and I want to know the occurence of each one of these in the randomized samples. For the example I gave at the beginning of this thread, I assigned each one an indetification number to make it easier to explain, but this is actually what elements are in my population.
So for my code then use
POP = 1:length(cell_array);
and index ito the cell array with the return from RANDSAMPLE, if needed. It looks like you are simply investigating the properties of that function....
adding this works perfectly but it still doesn't give me quite what I am looking for. From the result I got, it looks like all elements are present in the random samples, and there is no single time that one element was not in x. Anyway, thank you so much for your help, I really appreciate it. I'll try to figure out what to do from what you've explained so far. thank you.

Sign in to comment.

More Answers (1)

Why don't you just get the histogram with hist() or histc()?

6 Comments

I tried that but it didn't work. Plus, I need the actual values for later analyses.
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?
@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.
@Jan. This is the code:
for a = 1:1:1000
x = randsample(population,5)
histc(x)
end
And this is the error message I got:
??? Error using ==> histc
Not enough input arguments.
And for hist:
??? Error using ==> hist at 45 Input arguments must be numeric.
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...
@ Matt. I see what you mean, but still I get 1000 answers (the number of my iterations in the "for loop"), and it does not give how many of each element is in the vector answer.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!