Reduce the Number of Intensity Levels of a Grayscale Image in MATLAB
I think by number of levels you want the image full scale grey-scale to be divided piece-wise into given number of levels. 
For example: -


If number of levels = 2, then you want only two grey-scales in your image i.e. (0 and 128) or (128 and 255) depending upon if you are using floor or ceil within the range
If number of levels = 4, then you want 4 different levels in your image i.e. (0, 64, 124, 192)


Solution - 
This operation can be done in a single line 

new_img = ceil(img_gray./step)*step;

Here I have divide the entire range into desired number of parts (levels) with size as step and used ceil function to restrict the result to the upper bounds (you can use floor for lower bounds)

Full Code - 

img_color = imread('peppers.png');
img_gray = rgb2gray(img_color);
imshow(img_gray);

[rows, cols] = size(img_gray);
noOfDesiredIntensityLevels = 2;
step = ceil(255/(noOfDesiredIntensityLevels - 1));

new_img = ceil(img_gray./step)*step;

figure
imshow(new_img);


% optional code to show the levels
allSteps = 0;
currStep = 0;
while(currStep &lt; 255)
    currStep = currStep + step;
    allSteps = [allSteps currStep];
end

allSteps


Edit 1 - I have included few extra lines which shows you the different levels (steps) in an array. Its just optional but nice to know the exact values of the levels.