MATLAB: Scatter plot with different colours

scatterplot scatter gscatter colour

Hopefully this is possible to be done.
Say I have an 10×2 array called "matrix1" and another 10×1 array called "matrix2".
"matrix2" is only made up of 1's and 2's
Using scatterplot I can plot matrix1(:,1) vs matrix1(:,2) easily like so,
scatter(matrix1(:,1),matrix1(:,2))
BUT what I want is to use "matrix2" to colour code the plots. For example if a row in "matrix2" shows a "1" the corresponding row number in "matrix1" is blue on the scatter plot and if "matrix2" shows a "2" the corresponding row number in "matrix1" is red on the scatter plot.
Any ideas would be much appreciated. I have looked at gscatter and groupings but getting a bit lost!
Craig

Best Answer

One input arg of scatter() is a list of colors for the various data points. So just make up a colorlist and pass it in, something like
myColors = zeros(size(matrix1, 1), 3); % List of rgb colors for every data point.
rowsToSetBlue = matrix2 == 1;
rowsToSetRed = matrix2 == 2;
myColors(rowsToSetBlue, :) = [0,0,1];
myColors(rowsToSetRed, :) = [1,0,0];
I haven't test that - it's just off the top of my head. If the last two lines don't work then you might have to use repmat() to make them exactly length(rowsToSetBlue) rows tall.