How can I count the number of occurences of elements within vectors from randsample?
1 回表示 (過去 30 日間)
古いコメントを表示
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]?
採用された回答
Matt Fig
2012 年 11 月 29 日
編集済み: Matt Fig
2012 年 11 月 29 日
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 件のコメント
Matt Fig
2012 年 11 月 29 日
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....
その他の回答 (1 件)
Image Analyst
2012 年 11 月 29 日
Why don't you just get the histogram with hist() or histc()?
6 件のコメント
Matt Fig
2012 年 11 月 29 日
編集済み: Matt Fig
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...
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!