フィルターのクリア

count how many times the element occur in a list

2 ビュー (過去 30 日間)
pammy
pammy 2013 年 3 月 13 日
like i have the following code for the above:
function count
clc;
a=[1 2 3 3 4 5 5 5 5 5 6 6 6 6];
u=unique(a);
for n=1:length(u)
answer(n,:)=[u(n) length(find(a==u(n)))];
end
disp(answer);
end
output is:
1 1
2 1
3 2
4 1
5 5
6 4
i want to find the maximum value from the list (1 1 2 1 5 4) i.e from the 2nd column of the output...
2nd column of output displays the number of times the given element occurs.
can anyone tell me how to solve it?

採用された回答

Walter Roberson
Walter Roberson 2013 年 3 月 13 日
[maxcount, maxidx] = max(answer(:,2));
answer(maxidx,1), 'occurred', maxcount
  1 件のコメント
pammy
pammy 2013 年 3 月 14 日
thank u so much sir...

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 3 月 13 日
That's just the histogram:
% Create sample data.
a=[1 2 3 3 4 5 5 5 5 5 6 6 6 6 9 9 11 11 18]
% Get the histogram with bins every 1 wide from 1 to the max value of a.
numberOfBins = max(a)-min(a)+1 % Assumes integer a values.
counts = hist(a, numberOfBins)
% Find out where the histogram is maximum.
[maxValue, maxIndex] = max(counts)
% Print out information to the command line.
fprintf('The most frequently occurring number is %d, which occurs %d times.\n',...
maxIndex, maxValue);
In the command window:
a =
1 2 3 3 4 5 5 5 5 5 6 6 6 6 9 9 11 11 18
numberOfBins =
18
counts =
1 1 2 1 5 4 0 0 2 0 2 0 0 0 0 0 0 1
maxValue =
5
maxIndex =
5
The most frequently occurring number is 5, which occurs 5 times.
  2 件のコメント
pammy
pammy 2013 年 3 月 14 日
編集済み: pammy 2013 年 3 月 14 日
shows error in line 6
error using hist
Too many input arguments.
Error in hist (line 6)
counts = hist(a,numberOfBins)
Image Analyst
Image Analyst 2013 年 3 月 14 日
You probably redefined hist to be a variable in your program, thus blowing away the built-in function. What do these say:
whos hist
which -all hist

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by