MATLAB: Finding frequency of values in an array

doublefrequencymatrix arraysumvalue

Hi there
I have a 128x128x16 double array. Firstly, I would like to get the total number of values < zero in this array. Then, i would like to get the sum of the frequency of occurance of each value in this array. The array name is Phase.
thanks

Best Answer

One approach:
negPhase = nnz(Phase < 0); % Number Of Values < 0
[uPhase,ia,ic] = unique(Phase);
tally = accumarray(ic, 1);
freqOccurrence = [uPhase, tally]; % Frequency Of Occurrenct Of Each Value
If you do not want the results sorted (since the unique function does this by default), use:
[uPhase,ia,ic] = unique(Phase, 'stable');
instead. The rest of the code is unchanged. (Note that this will conform to the MATLAB linear matrix indexing convention, so it will be a column vector, not a matrix.)