2

The properties of my figure I want to build are the following:

The figure shows 200 points. The points above the diagonal should be shown with red stars and the one below the diagonal, with blue triangles.

This is what I've managed to do so far

x=[0 1];
y=[0 1];
line(x,y, 'linewidth', 1);
hggroup = scatter(rand(100,1),rand(100,1));
axis tight;
axis square;
title('Scatterplot')

Could you help me with that? Thanks in advance.

Alex Encore
  • 299
  • 1
  • 13
  • 26

1 Answers1

4

How about this:

line([0 1],[0 1], 'linewidth', 1);
hold on

x = rand(100,1);
y = rand(100,1);
idx = y>x;

scatter(x(idx),y(idx),'r*');
scatter(x(~idx),y(~idx),'b^');

axis tight;
axis square;
title('Scatterplot')
foglerit
  • 7,792
  • 8
  • 44
  • 64
  • This works perfectly! please, could you explain what the idx is and how is the '~idx' working exactly? – Alex Encore Feb 15 '12 at 15:44
  • I do know that a '~' is a logical NOT, but how is this working exactly? Thanks – Alex Encore Feb 15 '12 at 15:48
  • 1
    idx is an logical array indicating which values of y are > x. ~idx is the inverse of that, so all the pairs for which y < x. x(idx) returns an array containing only the values of x at indices for which idx is 1 – Jonathan Feb 15 '12 at 15:50
  • Thanks @Jonathan for the great explanation. In MATLAB, logical indexing is a very efficient way of accessing subsets of arrays. – foglerit Feb 15 '12 at 21:06