MATLAB: Plot points in xy-plane with different colours

colourplotshading

I have a function implemented in MATLAB which for each point in [-2;2] x [-2;2] (i. e. for -2 ≤ x,y ≤ 2) returns exactly one of three possible outputs. I want to plot the function such that every point in this square is coloured in one of three different colours (for example red, blue or green), based on what the output of the function was. Of course, colouring every point is not possible, which is why I am specifying a width of the grid of points beforehand. My code looks like this:
for x = -2:width:2
for y = 2:-width:-2
%function f(x,y) with one of 3 different outputs here
if (f(x,y) = output 1) %colour point (x,y) green
if (f(x,y) = output 2) %colour point (x,y) red
if (f(x,y) = output 3) %colour point (x,y) blue
end
end
How can I implement this colouring? In the end, I want a plot of the xy-plane in the given area (including axes) with points in different colours based on the above procedure.
Additional question: The function is actually a fixed-point iteration which is stopped as soon as the result is closer to either output 1, 2 or 3 than a tolerance specified in advance. Is it possible to alter the "brightness" of the point in my plot based on how many steps the iteration takes to fulfill the break criterion?

Best Answer

One option:
figure(1)
hold on
for x = -2:width:2
for y = 2:-width:-2
%function f(x,y) with one of 3 different outputs here
if (f(x,y) = output 1) %colour point (x,y) green
scatter(x,y,'pg', 'MarkerFaceColor','g')
elseif (f(x,y) = output 2) %colour point (x,y) red
scatter(x,y,'pr', 'MarkerFaceColor','r')
else (f(x,y) = output 3) %colour point (x,y) blue
scatter(x,y,'pb', 'MarkerFaceColor','b')
end
end
end
hold off
Some variation of it should give you the result you want. (I like to plot stars. Change the marker to your preference.)
NOTE This is UNTESTED CODE. You will have to experiment with it.