Q: Convesion vector Index exceeds array bounds - matlab I build some program in Matlab for "Histograma matching".
When I'm trying to implement the function "conVector" I get the error 
"Index exceeds array bounds." anyone can help me with this error?
Here is my full code. Thank you!
function [newImage] = histShape (srcimg,destimg)
%find the histogram of the image

src = imgHist(srcimg);
dest = imgHist(destimg);

sna = normalizationHist(src);
dna = normalizationHist(dest);

conVector(sna,dna);

end

function [Hist] = imgHist (img)
[Rows,Cols] = size(img);
Hist = zeros(1,256);
for i=1:Rows
    for j=1:Cols
        Hist(img(i,j)+1)=Hist(img(i,j)+1)+1;
    end
end

end


function [Ahist] = normalizationHist (hist)
[Rows,Cols] = size(hist);
Ahist = hist;
for i=2:256
    Ahist(i)=Ahist(i-1)+hist(i);
end

Ahist = Ahist/(Rows*Cols);

end

function [cv] = conVector(SNA,DNA)
cv=zeros(1,257);
s = 1;
d = 1;
while s<=256
    if DNA(d)<SNA(s)
        d = d+1;
    else
        cv(s)=d;
        s = s+1;

    end
end
end


A: If all values in DNA(d:end) are smaller then the value in SNA(s) than the loop keep adding 1 to d but not to s, and finally goes out of bound because it conditioned only by s.
I guess you should either take the s = s+1 out of the inner condition, so it will be executed on every iteration, or add a condition on d to the loop, or convert it to a for loop.
