Filter löschen
Filter löschen

How can I count the number of occurences of elements within vectors from randsample?

1 Ansicht (letzte 30 Tage)
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 Kommentar
Jan
Jan am 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]?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt Fig
Matt Fig am 29 Nov. 2012
Bearbeitet: Matt Fig am 29 Nov. 2012
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 Kommentare
Matt Fig
Matt Fig am 29 Nov. 2012
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....
Onja Razafindratsima
Onja Razafindratsima am 29 Nov. 2012
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 29 Nov. 2012
Why don't you just get the histogram with hist() or histc()?
  6 Kommentare
Matt Fig
Matt Fig am 29 Nov. 2012
Bearbeitet: Matt Fig am 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
Onja Razafindratsima am 29 Nov. 2012
@ 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by