How to Generate a vector map for histogram equalization
Your code is halfway correct; it creates the cumulative distribution function (CDF), but the map isnt quite there yet. 

First you get the histogram using imhist(), but we want to convert this to probabilities (the likelihood that each greyscale value occurs at a pixel). mapVec gives us integer counts, so just divide by the number of pixels in the image. 

mapVec=imhist(Im);
mapVec=mapVec / prod(size(Im));
EqVecMap = mapVec;


The next part calculating the CDF is correct (except for mapVec(i), I changed that to mapVec(j))

for i = 1:size(mapVec)
    EqVecMap(i)=0;
    for j = 1:i
        EqVecMap(i)=EqVecMap(i)+mapVec(j);
    end
end


But we're not done yet. EqVecMap is a CDF, so it's a double vector with max value is 1. However, our pixel values are INTEGERS with max value 255. So just

mapVec=round(EqVecMap*255);


mapVec is now a mapping function from old greyscale value (index 'i') to a new value (mapVec(i)). 

** I just saw havakok's answer (but I cant comment bc reputation), and I think it does basically the same thing. Only difference is you need to multiply by 255, not 256, since our max greylevel is 255 (the range includes 0, remember).