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

1 Ansicht (letzte 30 Tage)
Deepayan Bhadra
Deepayan Bhadra am 20 Feb. 2018
Kommentiert: Deepayan Bhadra am 20 Feb. 2018
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.

Akzeptierte Antwort

Cam Salzberger
Cam Salzberger am 20 Feb. 2018
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 Kommentar
Deepayan Bhadra
Deepayan Bhadra am 20 Feb. 2018
Thanks, Cam! Pretty much what I needed and I will also check the clustering bit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Statistics and Machine Learning Toolbox 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!

Translated by