Scatter Plot of 2 dimensional vectors

14 次查看(过去 30 天)
Hello!! I'm asking a question for my Machine Learning class.
The simple question is: how can I plot a collection of 2x1 vectors as points??
Here is the background information:
I have extracted 2 features and created a 2 by 1 vector (aka x = [feature1; feature2]) for each object being classified. Then, I stored each of these vectors in a cell array.
I have an array containing 7,000 2x1 feature vectors for Class 1 and I have another array containing 6,000 2x1 feature vectors for Class 2.
I would like to make a scatter plot of these 2x1 vectors, where data points (the vectors) from Class 1 are red and data points from Class 2 are green.
I've been attempting different scatter(), plot() functions, but none have outputted the result I need.
Thank you so much!!!

回答(2 个)

Walter Roberson
Walter Roberson 2017-1-29
编辑:Walter Roberson 2017-1-29
p1 = horzcat( Class1{:} ) .';
p2 = horzcat( Class2{:} ) .';
pointsize = 20;
h1 = scatter( p1(:,1), p1(:,2), pointsize, 'r');
hold on
h2 = scatter( p2(:,1), p2(:,2), pointsize, 'g');
hold off
legend( [h1, h2], {'Class 1', 'Class 2'})

mizuki
mizuki 2017-1-29
Convert the cell array into double first with cell2mat. That makes easy for you to plot data. You can set the color of the data points with the third input argument of SCATTER.
x{1} = rand(2,1); % 2x1
x{2} = rand(2,1);
x{3} = rand(2,1);
y{1} = rand(2,1); % 2x1
y{2} = rand(2,1);
y{3} = rand(2,1);
% Convert cell to double
x_mat = cell2mat(x); % 2x3
y_mat = cell2mat(y); % 2x3
% plot data with scatter
figure
scatter(x_mat(1,:), x_mat(2,:))
hold on;
scatter(y_mat(1,:), y_mat(2,:), 'g')
hold off;
grid on;

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by