function [r1,r2] = listvals(A,plotflag) % LISTVALS List the values in an array, and how many times they occur % M = LISTVALS(A) Returns a list of the values in the array A, and % the number of times they occur. The first column of M contains a % list of the values in the array A, sorted into ascending order, % and the second column of M contains the number of times each value occurs. % That is, the value M(i,1) occurs in the array A M(i,2) times. % You can use the form [VALS,COUNTS] = LISTVALS(A) to get the values % and their counts as two separate return values. % Finally, if you give a second argument which is not zero, LISTVALS % will make a bar plot of the values and their counts. E.g. LISTVALS(A,1);. % % Written by David Hiebeler, http://www.math.umaine.edu/faculty/hiebeler tmpA = A(:)'; % if A is multidimensional, convert it into a 1D row vector retval = []; % start with empty array while (size(tmpA, 2) ~= 0) % while we still have some elements left % find minimum of remaining elements minval = min(tmpA); % see how many times it appears in the array mincount = sum(tmpA == minval); % add this info to return value retval = [retval ; minval mincount]; % now remove all copies of minval from array tmpA = tmpA(find(tmpA ~= minval)); end if (nargout == 2) r1 = retval(:,1); r2 = retval(:,2); else r1 = retval; end if (nargin == 2) if (plotflag) bar(retval(:,1), retval(:,2)) end end