Perhaps this can be solved without loops, but my code looks like this:
M = [7 9 6 ; 8 7 9 ; 7 6 7 ;]; number = unique(M(:)); frequency = hist(M(:), number)'; map = containers.Map(number, frequency); [height width] = size(M); result = zeros(height, width); %allocate place for i=1:height for j=1:width result(i,j) = map(M(i,j)); end end
source share