RGB-Triplett - Matrix Error
3 views (last 30 days)
Show older comments
My Code is not working. Does anyone have an Idea.
It has to do with the color code!
% Größe der quadratischen Matrix
size_matrix = 50;
% Erzeugen einer quadratischen Matrix mit zufälligen Werten
X = rand(size_matrix, size_matrix);
% Umwandeln der quadratischen Matrix in einen Vektor
X_vector = X(:);
% GMM-Clusteranalyse durchführen
num_clusters = 5; % Anzahl der Cluster für GMM
options = statset('MaxIter', 1000); % Anzahl der Iterationen erhöhen
gm = fitgmdist(X_vector, num_clusters, 'Options', options);
% Cluster-Zuweisungen erhalten
idx = cluster(gm, X_vector)
unique(idx)
% Konvertiere Cluster-Indizes in RGB-Farben
cmap = jet(num_clusters); % Farbkarte für Cluster
colors = cmap(idx, :) % Farben für jeden Punkt basierend auf dem Cluster-Index
% Visualisierung der Cluster
figure;
scatter3(X(:,1), X(:,2), X(:,3), 10, colors, 'filled');
xlabel('Feature 1');
ylabel('Feature 2');
zlabel('Feature 3');
title(sprintf('GMM Cluster mit %d Clustern', num_clusters));
colorbar;
Error using scatter3
Color must be one RGB triplet, an m-by-3 matrix of RGB triplets with one color per scatter point,
or an m-by-1 vector with one value per scatter point.
>>
1 Comment
Dyuman Joshi
on 21 Feb 2024
You have 50 points for the scatter plot, where as you have 2500 colors. That mapping can not be done.
As the error message mentions, if you supply a mx3 matrix as RGB triplet color values, m must be equal to the number of points plotted (or scattered, lol).
Answers (1)
Ninad
on 27 Feb 2024
Hi Alexander,
The "scatter3" function expects the color matrix to have the same number of rows as there as data points to plot. However, you are trying to plot a 50 x 50 matrix "x" as a 3D scatter plot, but you are passing a color matrix "color" that has 2500 x 3 elements.
To fix this error you can use the "ind2sub" function to generate three coordinates for each point. The "ind2sub" function converts linear indices to subscript.
Please refer the following documentation for details about "ind2sub" function:
Please use the following code to visualize the Cluster:
figure;
[row, col] = ind2sub([size_matrix, size_matrix], 1:numel(X));
% Now row, col, and X_vector can be used as coordinates for scatter3
scatter3(row, col, X_vector, 10, colors, 'filled');
Hope this helps.
Regards,
Ninad
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!