How to plot the sample mean in a scatter plot?
Show older comments
Hi; Yesterday I had a question about the gscatter command and with the help I got here, I got the results that I wanted:
% Class 1
x1 = [16; 18; 20; 11; 17; 8; 14; 10; 4; 7;];
y1 = [13; 13; 13; 12; 12; 11; 11; 10; 9; 9];
class1 = [x1 y1];
% Class 2
x2 = [8; 9; 6; 8; 5; 7; 4; 6; 4; 3];
y2 = [8; 7; 7; 6; 5; 5; 4; 3; 2; 2];
class2 = [x2 y2];
% Class 3
x3 = [19; 19; 17; 17; 16; 14; 13; 13; 11; 11];
y3 = [6; 3; 8; 1; 4; 5; 8; 1; 6; 3];
class3 = [x3 y3];
figure(1)
x = [x1;x2;x3];
y = [y1;y2;y3];
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))]; % Grouping variableis the class number for each observation in Band 1 and Band 2
gscatter(x,y,grp,'brg','.xo'), xlabel('Band 1'), ylabel('Band 2'), title('Data of Each Class in a Two-Dimensional Vector Space'), legend('Class 1', 'Class 2', 'Class 3')
Now, I need to plot the sample mean of each class on the plot but I can't seem to find what I'm doing wrong. The code is as follows:
% b - Compute the sample means and sample covariance matrices for each class and plot the means in the same vector space with the data
% Class 1
M1 = mean(class1) % Sample Mean of Class 1
C1 = cov(class1) % Covariance Matrix of Class 1
% Class 2
M2 = mean(class2) % Sample Mean of Class 2
C2 = cov(class2) % Covariance Matrix of Class 2
% Class 3
M3 = mean(class3) % Sample Mean of Class 3
C3 = cov(class3) % Covariance Matrix of Class 3
figure(2)
x = [x1;x2;x3];
y = [y1;y2;y3];
grp = [ones(size(x1));2*ones(size(x2));3*ones(size(x3))]; % Grouping variableis the class number for each observation in Band 1 and Band 2
gscatter(x,y,grp,'brg','.xo'), xlabel('Band 1'), ylabel('Band 2'), title('Data of Each Class in a Two-Dimensional Vector Space with the Sample Means')
hold on
plot(M1(1,1),M1(1,2),'b')
hold on
plot(M2(1,1),M2(1,2),'r')
hold on
plot(M3(1,1),M3(1,2),'g'),legend('Class 1','Class 2','Class 3','Mean_Class 1','Mean_Class 2','Mean_Class 3')
The mean of each class is not shown in the figure. How can I plot the sample mean of each class?
Accepted Answer
More Answers (0)
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!