MATLAB: I want to calculate the equation:

digital image processingfor loopimage processingMATLABmatricesmatrix

Where Ik is the kth gray level, nk are number of pixels of gray level, N is total number of pixels in input test image. and i try to write the code but i need to your help please , it`s Execution in wrong .
clear all
clc
%%Real image
%img = imread('imagetest.jpg');
% img_gray = img(:,:,1);
%%just example with an array:
img_gray= [5 6 7 8 ; 2 8 6 3 ; 4 0 4 2 ; 5 0 1 0];
%%Start processing:
[rows cols]=size(img_gray);
A=img_gray;
R = size(A,1);
C = size(A,2);
N=rows * cols % i think that N must be length(symb)
L = R
% All pixels without repetition:
% frequency of each pixel:
symb =unique(A)
nk = [symb,histc(A(:),symb)];
count = 0;
for l=1:255
for i= 1:R
for j = 1:C
if A(i,j)== L
count = count +1;
I(L) = count;
end
end
end
end
% calculate the sum :
sumt = 0;
for i=1:rows
for j=1:cols
sumt = sumt + (A(i,j)*nk)
end
end
sumt=sumt/N
XT = sum(sumt);

Best Answer

I = [5 6 7 8 ; 2 8 6 3 ; 4 0 4 2 ; 5 0 1 0];
[l, ~, iI] = unique(I);
nk = accumarray(iI, 1);
XT = l'*nk/numel(I)