I want to count the number of values in the array. I have a code which works:
Range = [1:10^3];% [1:10^6];
N = 10^2;% 10^8
Data = randi([Range(1),Range(end)],N,1);
Counts = nan(numel(Range),1);
for iRange = 1:numel(Range)
Counts(iRange) = sum(Data==Range(iRange));
end
Could you help me to make this code faster?
I feel that it should be via unique or hist, but I could not find a solution.
N = histcounts(Data,Range)
gives me 999 numbers instead of 1000.
histor even betterhistcountsshould do the job. Have you tried them?histcounts, as the documentation states, are the edges of the bins.[0,1,2]will return 2 values: the count of the numbers between 0-1 and the ones between 1-2. Just define your range properly. Perhaps0:10^3?