I am doing Run Length Coding in Matlab and so far I have implemented the zigzag algorithm and got an array RunLengthCoding:
RunLengthCoding =
(32, 6, -1, -1, 0, -1, 0, 0, 0, -1, 0, 0, 1, 0, 0,..., 0)
Now I need to run length code this such that I get:
(0,6) (0,-1) (0,-1) (1,-1) (3,-1) (2,1) (0,0)
This is (length,value), for example (0,6) because there is no 0 and we are at value 6, then when we meet the first 0 we get (1,-1) since there is one 0 and the value after it is -1.
My attempt:
RunLengthCoding(1)=[]; %to remove first DC component
relMat = [];
N = 0;
for i = 1:length(RunLengthCoding)-1
if RunLengthCoding(i)==0;
if RunLengthCoding(i)==RunLengthCoding(i+1)
N = N + 1;
else
valuecode = RunLengthCoding(i);
lengthcode = N;
relMat = [relMat; lengthcode valuecode];
N = 1;
end
else
relMat=[relMat; 0 RunLength(i)];
end
I know that this does not run! But that is what I have managed so far
This should do the trick: