for loop to update frequency count
3 ビュー (過去 30 日間)
古いコメントを表示
i have a solution to my problem but it does not use a for loop and i think i can get a more complete answer with one. i have a decent sized matrix, lets say 500 x 500, containing values between 0 and 1000. i want to find the frequency that each value appears in my matrix and get back a 1001 X 2 matrix, column 1 are the values 0 through 1000 and the second column is the count. i know a very simple way to get the counts of the values that appear at least once, but i want to include the unused values as well. here is what i have:
function Y = test(X)
ux = unique(X);
x = [ux,histc(X(:),ux)];
a = [0:1:min(ux)-1]';
b = zeros(min(ux), 1);
n = horzcat(a,b);
F = vertcat(n,x);
Here is as far as i've gotten in my for loop:
num_rows = size(X,1)
num_cols = size(X,2)
% loop through all values
for i = 1 : 1 : num_rows
for j = 1 : 1 : num_cols
F(X(i,j) + 1,2) =
end
end
can anyone point me in the right direction?
2 件のコメント
Walter Roberson
2015 年 9 月 20 日
You have values between 0 and 1000 but you expect only 501 different values, not 1001. And you expect some of the values might be missing from X. In order to list the missing values, we need to know which 501 values to expect might be present and which are not expected to be present. For example do you expect only even numbers? Are all of the entries integers? What should be done if a value in X is not one of the 501 that you expect?
回答 (2 件)
Leo Simon
2015 年 9 月 20 日
This gets close I think
A = floor(rand(500)*1001);B=A(:);I = find(B<501); [N,X] = hist(B(I),501);
X contains the centers of the 501 bins of the histogram.
So I think,
[ floor(X), N ]
seems to be what you need?
0 件のコメント
Walter Roberson
2015 年 9 月 21 日
count_matrix = accumarray(X(:)+1, 1, [1001 1]); %provided that the entries are integers
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!