Color for points in scatter plot based on quadrants
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
matrix=load('Data');
x = matrix(:,1);
y = matrix(:,2);
matrix(x>0 & y>0,3) = 1;
matrix(x<0 & y>0,3) = 2;
matrix(x<0 & y<0,3) = 3;
matrix(x>0 & y<0,3) = 4;
if x<-128 or x>128 or y<-128 or y>128
'Error:Value exceeds limit'
end
if matrix(:,3)==1 or matrix(:,3)==3
scatter(x,y,'.r')
elseif matrix(:,3)==2 or matrix(:,3)==4
scatter(x,y,'.b')
end
title('Variables and Their Quadrants')
xlabel('X Values')
ylabel('Y Values')
So here's my problem: what I need to do is have any points in quadrants 1 and 3 to be red and any points in quadrants 2 and 4 to be blue. I am using a third row to determine the quadrant, but when I plug that row into a if statement, the script pretends it does not exist. How do I fix this problem?
0 Kommentare
Akzeptierte Antwort
Kelly Kearney
am 4 Mai 2015
You actually have several syntax errors in that script, but they're never being reached because of the way you've structured your if statements.
An if-statement evaluated on a vector only executes if all values in that vector are true. So, for example, when you write
if x < -128
it's the same as
if all(x < -128)
Because that statement is false, the rest of the line isn't evaluated (which is why you don't immediately get an error, since the rest of the statement isn't valid command). You should use any instead.
To get what you want, simply pass the third column as your color data, and set the colormap accordingly:
matrix = randn(50,2);
x = matrix(:,1);
y = matrix(:,2);
matrix(x>0 & y>0,3) = 1;
matrix(x<0 & y>0,3) = 2;
matrix(x<0 & y<0,3) = 3;
matrix(x>0 & y<0,3) = 4;
if any(x < -128 | x > 128 | y < -128 | y > 128)
error('Value exceeds limit');
end
scatter(x,y,[],matrix(:,3),'filled');
colormap([1 0 0; 0 0 1; 1 0 0; 0 0 1]);
set(gca, 'clim', [0.5 4.5]);
title('Variables and Their Quadrants')
xlabel('X Values')
ylabel('Y Values')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Colormaps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!