Borrar filtros
Borrar filtros

RGB-Triplett - Matrix Error

6 visualizaciones (últimos 30 días)
Alexander Dreier
Alexander Dreier el 21 de Feb. de 2024
Respondida: Ninad el 27 de Feb. de 2024
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)
idx = 2500×1
3 4 4 5 4 5 2 4 2 4
unique(idx)
ans = 5×1
1 2 3 4 5
% 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
colors = 2500×3
0.5000 1.0000 0.5000 1.0000 1.0000 0 1.0000 1.0000 0 1.0000 0.5000 0 1.0000 1.0000 0 1.0000 0.5000 0 0 1.0000 1.0000 1.0000 1.0000 0 0 1.0000 1.0000 1.0000 1.0000 0
% Visualisierung der Cluster
figure;
scatter3(X(:,1), X(:,2), X(:,3), 10, colors, 'filled');
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.
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 comentario
Dyuman Joshi
Dyuman Joshi el 21 de Feb. de 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).

Iniciar sesión para comentar.

Respuestas (1)

Ninad
Ninad el 27 de Feb. de 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

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!