Classifiying the data points
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi i have a data set such as;
[x y z d], (100x4) matrices
x,y and z are coordinates and the d is the measure of a distance.Every d is corresponding with one point (x, y, z)
I want to create 10 classes from d and colorize the points (x,y,z) according to this classes and plot it
is anyone have idea?
Thank you
채택된 답변
Image Analyst
2019년 7월 2일
Try kmeans with 10 classes, and operating on the d values:
data = rand(100, 4)
x = data(:, 1);
y = data(:, 2)
z = data(:, 3);
d = data(:, 4);
numClasses = 10
% Find classes based on 4th column.
indexes = kmeans(d, numClasses);
% Make up a colormap with 10 colors
cmap = jet(10);
% Assign a color to each data point based on it's class
markerColors = zeros(10, 3); % 10 colors.
for k = 1 : size(data, 1)
thisClassNumber = indexes(k)
markerColors(k, :) = cmap(thisClassNumber, :);
end
scatter3(x, y, z, 50, markerColors, 'filled');
fontSize = 20;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);

There is a marker at each (x,y,z) location and its color depends on which of the 10 classes that point's "d" value got classified into.
댓글 수: 10
Thank you for your answer but i get the error given below;
Error using scatter3 (line 45)
Error in color/linetype argument.
Error in siniff (line 63)
scatter3(x1, y1, z1, 50, markerColors, 'filled ');
What does this do:
whos markerColors
What does that report to the command window?
It seems like markerColors doesn't have as many colors (rows) as there are elements in your x1 vector.
x1 have 100x1. but i need to classify "d" in 10 class according to its value. x1, y1 and z1 are only the coordinates of data point. I don't want to classify the x1, y1 or z1 they are only coordinates.So i need only ten colors for classifying "d" and show this color on related (x1 y1 z1)
Thank you
Image Analyst
2019년 7월 3일
That's exactly what I did. Look at the call to kmeans(). Do you see me passing x, y, or z into kmeans()? No. You only see me passing in d. Why do you think I classified x, y, and z? Show me where those are being passed in to kmeans().
Again, answer my question about whos markerColors. Better yet, attach your entire script and data file.
clc;clear;
x=xlsread('ping1.xlsx', 'A:A');
y=xlsread('ping1.xlsx', 'B:B');
z=xlsread('ping1.xlsx', 'C:C');
a=xlsread('ping2.xlsx', 'A:A');
b=xlsread('ping2.xlsx', 'B:B');
c=xlsread('ping2.xlsx', 'C:C');
xyz=[x y z];
abc=[a b c];
rng(1);
[idx1,C1] = kmeans(xyz,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(abc,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
for i=1:100
t(i)=i
end
t=t';
for i=1:100
u(i)=i;
end
u=u';
[dist,idx3] = pdist2(xyz, C1, 'euclidean', 'Smallest',1);
newVar = xyz(idx3 ,:);
plot3(newVar(:,1), newVar(:,2), newVar(:,3), 'bx');
text(newVar(:,1)+0.02,newVar(:,2),newVar(:,3),num2str(t),'FontSize',7);
hold on;
xlabel ('x - axis', 'fontsize', 12);
ylabel ('y - axis', 'fontsize', 12);
zlabel ('z - axis', 'fontsize', 12);
grid
[dist2,idx4] = pdist2(abc, C2, 'euclidean', 'Smallest',1);
newVar2 = abc(idx4 ,:);
%plot3(newVar2(:,1), newVar2(:,2), newVar2(:,3), 'r*');
%text(newVar2(:,1),newVar2(:,2),newVar2(:,3),num2str(u),'FontSize',7);
axis equal;
newVar3 = mean (newVar);
newVar4 = mean (newVar2),
newVar5 = (newVar3 + newVar4)/ 2;
%plot3(newVar5(:,1), newVar5(:,2), newVar5(:,3), 'go');
m=[newVar(:,1) newVar(:,2) newVar(:,3)];
n=[newVar2(:,1) newVar2(:,2) newVar2(:,3)];
o=[newVar5(:,1) newVar5(:,2) newVar5(:,3)];
d1 = pdist2(newVar5,newVar); % distance(s) (many) between O and X
[~,idx5] = pdist2(newVar2,newVar,'euclidean','smallest',1); % indices of nearest * for EACH X
d2 = pdist2(newVar5,newVar2(idx5,:)); % distance(s) between O and * (nearest to X)
D=d2 - d1
x1 = m(:, 1 );
y1 = m(:, 2 );
z1 = m(:, 3 );
numClasses = 10
% Find classes based on 4th column .
indexes = kmeans(D(:), numClasses );
% Make up a colormap with 10 colors
cmap = jet(10 );
% Assign a color to each data point based on it's class
markerColors = zeros(10, 3); % 10 colors .
for k = 1 : size(m, 1 )
thisClassNumber = indexes(k )
markerColors(k, :) = cmap(thisClassNumber , :);
end
scatter3(x1, y1, z1, 50, markerColors, 'filled ');
fontSize = 20 ;
xlabel('X', 'FontSize', fontSize );
ylabel('Y', 'FontSize', fontSize );
zlabel('Z', 'FontSize', fontSize);
Image Analyst
2019년 7월 3일
편집: Image Analyst
2019년 7월 3일
See, you're passing in the first 3 columns to kmeans, which are the xyz coordinates. I didn't do that. I extracted column 4 and passed in that because only column 4 is your "d" value that you want to do cluster analysis on. Then, you shouldn't have 100 classes since you want only 10. You should do something like:
d1=xlsread('ping1.xlsx', 'D:D');
d2=xlsread('ping2.xlsx', 'D:D');
numClasses = 10;
[idx1,C1] = kmeans(d1,numClasses,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(d2,numClasses,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
The aim of first kmeans usage is different. I created a new classed xyz called (m) and abc called (n), then i calculate the distance between m and closest n and called it "d". Now i try to classify the d values and show the color on m. it is a little bit complicated i know.
But if you remove % from;
%plot3(newVar2(:,1), newVar2(:,2), newVar2(:,3), 'r*');
and
%plot3(newVar5(:,1), newVar5(:,2), newVar5(:,3), 'go');
it may be more clear
thanks
Image Analyst
2019년 7월 4일
You added a space after "filled". It makes a difference. Somehow when you were altering the line of code I gave you, you added the space.
If you use 'filled ' instead of 'filled' then you will get the error you got. If you don't, you won't get the error.
Thank you for the answer. the problem was space. Now it is working. I have one more question, is it possible to add a legend abour classification...
thank you again for your helps
Image Analyst
2019년 7월 4일
I think you'd have to do it manually with text().
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 k-Means and k-Medoids Clustering에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
