how to plot points in different colors based on class?
조회 수: 9 (최근 30일)
이전 댓글 표시
x1 x2 class
-1.7986 -1.6730 1
-1.0791 -0.5937 1
-0.5995 0.7556 1
1.0791 -1.4032 1
0.1199 0.2159 1
-0.3597 -0.4857 -1
0.3597 -1.5651 -1
-0.5995 -0.4857 -1
-0.1199 0.3238 -1
-1.5588 -0.4857 -1
how to plot x1 and x2 based on the class,class 1 and class -1 with different colors
댓글 수: 0
채택된 답변
Image Analyst
2014년 9월 29일
Try this:
clc;
m=[...
-1.7986 -1.6730 1
-1.0791 -0.5937 1
-0.5995 0.7556 1
1.0791 -1.4032 1
0.1199 0.2159 1
-0.3597 -0.4857 -1
0.3597 -1.5651 -1
-0.5995 -0.4857 -1
-0.1199 0.3238 -1
-1.5588 -0.4857 -1]
x1 = m(:,1);
x2 = m(:, 2);
classes = m(:,3);
sizes = 15 * ones(1,length(x1));
% Plot first class
scatter(x1(classes == 1), x2(classes == 1), 150, 'b', '*')
% Plot second class.
hold on;
scatter(x1(classes == -1), x2(classes == -1), 120, 'r', '*')
댓글 수: 1
Joseph Cheng
2014년 9월 29일
on the same lines of scatter+hold on if you have more than 2 classes and it would be too tedious to code for each case the following can be used to plot in a for loop
x1 = rand(10,1);
x2= rand(10,1);
class = (-1).^randi(2,10,1).*randi(10,10,1);
uClass = unique(class)
pntColor = hsv(length(uClass))
figure,hold on
for ind = 1:length(uClass)
scatter(x1(class == uClass(ind)), x2(class == uClass(ind)), 150, 'MarkerFaceColor',pntColor(ind,:),'Marker','*')
end
추가 답변 (1개)
Joseph Cheng
2014년 9월 29일
you can find which indexes have either class and plot accordingly.
x1 = rand(10,1);
x2= rand(10,1);
class = (-1).^randi(2,10,1);
figure,plot(x1(class==1),x2(class==1),x1(class==-1),x2(class==-1))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!