Assigning class +1/-1 to a 100x2 matrix

1 次查看(过去 30 天)
I have a 100x2 matrix D. A scatter plot is attached. I have a 100x1 vector 'c'. If a point in D is to the left, I want to assign it as +1 and -1 otherwise. I know it sounds easy and I was trying this snippet:
if D(1:100,1)<mean(D(:,1)) c(:,1) = 1; else c(:,1) = -1; end
but it somehow doesn't do the job. I think we don't even need an if loop. Thanks for your comments.

采纳的回答

Cam Salzberger
Cam Salzberger 2018-2-20
Hello Deepayan,
You probably are trying to use logical indexing, so you don't need the conditional.
c = ones(size(D, 1), 1);
c(D(:, 1) > mean(D(:, 1))) = -1;
On the other hand, you've got some very nice clusters here, and this code depends on you having roughly equal number of points in each cluster. Rather than mean, it would probably make sense to use some form of cluster analysis to figure out where the clusters are, and which points belong to which. kmeans is one of the most common methods, and would probably suffice here.
-Cam
  1 个评论
Deepayan Bhadra
Deepayan Bhadra 2018-2-20
Thanks, Cam! Pretty much what I needed and I will also check the clustering bit.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by