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

조회 수: 1 (최근 30일)
Deepayan Bhadra
Deepayan Bhadra 2018년 2월 20일
댓글: Deepayan Bhadra 2018년 2월 20일
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