Need Help with MATLAB Fuzzy Clustering - Trouble Implementing Example

11 visualizaciones (últimos 30 días)
Baptiste
Baptiste el 19 de Sept. de 2023
Comentada: Sam Chak el 20 de Sept. de 2023
Hello MATLAB Enthusiasts,
I hope you're all doing well. I've been working on a project where I'm trying to implement a fuzzy clustering algorithm, and I've encountered a roadblock while trying to adapt an example I found on the MATLAB website. Here's the link to the example: [Fuzzy C-Means Clustering Example](https://www.mathworks.com/help/fuzzy/fcm.html)
My Problem:
I'm working with a dataset named "aaaa.dat," which is a 58x3 matrix. My goal is to apply the Fuzzy C-Means (FCM) clustering algorithm to this dataset. However, I've been facing some issues while trying to run the provided example code.
Here's the Code I Tried:
%%
load aaaa.dat
options = fcmOptions(...
NumClusters=[2 3 4],...
Verbose=false);
[centers,U,objFun,info] = fcm(aaaa,options);
Nc = info.OptimalNumClusters;
info.ValidityIndex
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
index3 = find(U(3,:) == maxU);
figure
hold on
scatter3(aaaa(index1,1),aaaa(index1,2),...
aaaa(index1,3))
scatter3(aaaa(index2,1),aaaa(index2,2),...
aaaa(index2,3))
scatter3(aaaa(index3,1),aaaa(index3,2),...
aaaa(index3,3))
plot3(centers(:,1),centers(:,2),centers(:,3), ...
"xk",MarkerSize=15,LineWidth=3)
xlabel("Feature 1")
ylabel("Feature 2")
zlabel("Feature 3")
view([-11 63])
hold off
The Error I'm Encountering:
After running the code, I'm getting the following output in the command window:
ans =
0.0377 0.5137 0.2409
Index in position 1 exceeds array bounds. Index must not exceed 2.
Error in testCluster (line 28)
index3 = find(U(3,:) == maxU);
I've reviewed my code multiple times, but I'm unable to pinpoint what I might be doing wrong. It's quite frustrating, and I would greatly appreciate any insights, suggestions, or tips you can offer.
If you've worked with Fuzzy C-Means clustering in MATLAB or have any ideas on what might be causing the issue, please share your thoughts. Your assistance would be invaluable to me.
Thank you in advance for your help!

Respuestas (1)

Sam Chak
Sam Chak el 19 de Sept. de 2023
Please take a look at this example. The data set is specified as a matrix with 9 rows, each representing a data point. If the FCM algorithm finds 2 cluster centers, as in this data set, then the fuzzy partition matrix U will be returned as a matrix. Thus, in cases where the third row does not exist, the syntax 'index3 = find(U(3,:) == maxU)' will throw an index error.
aaaa = rand(9, 3)
aaaa = 9×3
0.3001 0.0600 0.5288 0.8650 0.9651 0.8758 0.7335 0.1340 0.6297 0.8012 0.2253 0.9183 0.5856 0.0330 0.9169 0.5910 0.0425 0.4886 0.5550 0.0694 0.2712 0.3138 0.7647 0.6876 0.5967 0.2476 0.8687
options = fcmOptions(...
NumClusters = [2 3 4], ...
Verbose = false);
[centers, U, objFun, info] = fcm(aaaa, options)
centers = 2×3
0.6145 0.8102 0.7867 0.5897 0.1133 0.6460
U = 2×9
0.1212 0.9002 0.0413 0.2501 0.1138 0.0420 0.1493 0.8307 0.1730 0.8788 0.0998 0.9587 0.7499 0.8862 0.9580 0.8507 0.1693 0.8270
objFun = 11×1
0.8854 0.7936 0.7487 0.6980 0.6685 0.6584 0.6558 0.6552 0.6551 0.6551
info = struct with fields:
NumClusters: [2 3 4] ClusterCenters: {[2×3 double] [3×3 double] [4×3 double]} FuzzyPartitionMatrix: {[2×9 double] [3×9 double] [4×9 double]} ObjectiveFcnValue: {[11×1 double] [17×1 double] [16×1 double]} ValidityIndex: [0.2876 0.5001 1.5358] OptimalNumClusters: 2
Nc = info.OptimalNumClusters;
info.ValidityIndex
ans = 1×3
0.2876 0.5001 1.5358
maxU = max(U);
index1 = find(U(1,:) == maxU)
index1 = 1×2
2 8
index2 = find(U(2,:) == maxU)
index2 = 1×7
1 3 4 5 6 7 9
index3 = find(U(3,:) == maxU)
Index in position 1 exceeds array bounds. Index must not exceed 2.
  2 comentarios
Baptiste
Baptiste el 20 de Sept. de 2023
Thank you very much for your response; I appreciate your assistance. It indeed makes perfect sense that the script should work for two clusters. However, I've come across a rather puzzling issue: I can execute the script successfully in MATLAB Online, but it fails to run in the desktop application. Upon running the script, I encounter the following error message:
>> testCluster
Unrecognized function or variable 'fcmOptions'.
Error in testCluster (line 15)
options = fcmOptions(...
I've double-checked the installation of the Fuzzy Logic Toolbox add-on, and it appears to be installed correctly. Despite this, I'm still unable to get it to work on the desktop version. Have you ever encountered a similar problem or do you have any insights on how to resolve it? Your guidance would be greatly appreciated.
Sam Chak
Sam Chak el 20 de Sept. de 2023
Hi @Baptiste, upon checking, the function was introduced in release R2023a. This probably explains why it threw the error message.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Clustering en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by