我正在开发一段 k-means 模糊代码。现在我想保存 statset('Display','iter');
显示的每次迭代的数据。请帮助我。
X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
opts = statset('Display','iter');
[cidx, ctrs] = kmeans(X, 2, 'Distance','city', ...
'Replicates',5, 'Options',opts);
plot(X(cidx==1,1),X(cidx==1,2),'r.', ...
X(cidx==2,1),X(cidx==2,2),'b.', ctrs(:,1),ctrs(:,2),'kx');
最佳答案
函数 diary
给出了一个虚拟解它可以将 matlab 控制台输出存储在文件中。
X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
opts = statset('Display','iter');
diary('output.txt') % # Whatever is displayed from now on is saved on 'output.txt'
[cidx, ctrs] = kmeans(X, 2, 'Distance','city', ...
'Replicates',5, 'Options',opts);
diary('off') % # logging is disabled
执行后,output.txt
将包含
iter phase num sum
1 1 40 96.442
2 1 8 79.7403
3 1 6 70.2776
...
您可能希望在每次运行时清理 output.txt
的内容,否则它只会在前一个日志之后附加新日志。
关于matlab - 如何在 Matlab 中将数据保存在 "opts = statset(' Display' ,'iter' );”显示的文件或矩阵中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817861/