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

 Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Jul. 2019

0 Stimmen

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);
0000 Screenshot.png
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 Kommentare

Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan am 2 Jul. 2019
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.
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan am 3 Jul. 2019
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
Image Analyst am 3 Jul. 2019
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
Image Analyst am 3 Jul. 2019
Bearbeitet: Image Analyst am 3 Jul. 2019
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);
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan am 3 Jul. 2019
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
Image Analyst am 4 Jul. 2019
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.
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan am 4 Jul. 2019
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
Image Analyst am 4 Jul. 2019
I think you'd have to do it manually with text().

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by