How to count number of occurences of each value in a vector and put the number beside the value as a new column?

조회 수: 22 (최근 30일)
I have vector (3079X1).
I want to count the number of occurrences of each value within the vector and put that number beside the respective value of the vector. I have tried using groupcount but it reorders the result in ascending order. I want to keep the same order as it was in the original vector.

답변 (3개)

Stephen23
Stephen23 2020년 4월 13일
>> V = randi(9,13,1)
V =
6
4
4
2
9
5
1
6
2
1
6
9
4
>> [N,X] = histc(V,unique(V));
>> M = [V,N(X)]
M =
6 3
4 3
4 3
2 2
9 2
5 1
1 2
6 3
2 2
1 2
6 3
9 2
4 3

darova
darova 2020년 4월 13일
Try this
a = randi(5,20,1);
x = a*0;
[a1,ix] = sort(a); % sort array to see how many duplicates
k = 0;
for i = 1:length(a)-1
if a1(i)~=a1(i+1)
x(k+1:i) = i-k; % write numer of duplicates
k = i; % position of last diplicate
end
end
x(k+1:end) = length(x)-k; % write last portion of duplicates
[~,ix1] = sort(ix); % restore original order
disp('sorted values with number of duplicates')
[a1 x]
disp('original values with numer of duplicates')
[a x(ix1)]
  댓글 수: 2
Adnan Habib
Adnan Habib 2020년 4월 14일
This one gives the error message as "Undefined operator '~=' for input arguments of type 'cell'."
I am very new to matlab and it is possible I did not provide some information regarding my data that might have been useful for you to help me out. Please let me know if you need any further information
Walter Roberson
Walter Roberson 2020년 4월 14일
You posted that you have a "vector"; it now appears that you have a cell array of some sort. Possibly it is a cell array of character vectors. Possibly it is a cell array of transfer functions. Possibly it is a cell array of function handles. We would need more information to advise you.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 4월 13일
Try this:
% Create sample data
v = randi(1000, 3079, 1)
% Now take a histogram:
edges = min(v) : 1 : max(v)+1;
hObject = histogram(v, edges) % Also displays the histogram
grid on;
% Create matrix with v as first column, and count as second column
m = [v, zeros(length(v), 1)];
% Assign the counts to each value.
for k = 1 : length(v)
% Get the value of the vector at this index.
value = v(k);
% Get the counts from the histogram for that value
% and stuff into the second column of m.
m(k, 2) = hObject.Values(value);
end
  댓글 수: 6
Image Analyst
Image Analyst 2020년 4월 14일
I just copied and pasted my code and it ran fine. Moreover, the first argument to min() is a numeric vector -- it's a double. So you must have altered my code somehow. But you forgot to attach your modified script. Please do so if you want me to fix it.
Adnan Habib
Adnan Habib 2020년 4월 14일
I have actually done something else to bypass the requirement of counting. So I don't need this anymore. But I really do appreciate all your help. Thanks a lot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by