Is there a way to initialise an array automatically inside while loop?

Hi all,
I am working on build clustering algorithm (Fixed-width)clustering. In every iteration inside while loop, some points need to be store in an array to use them in final plot. However, I do not know how can I store points that are belonge to a specific cluster in new array in every iteration. The target is showing all points with their clusters in a single plot.
Thank you

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 11 月 1 日
Abdulatif - if you just want to add some to an array at each iteration of a loop (in your case, a while loop), then you could try the following
% initialize the cluster points array to be empty
clusterPoints = [];
while true
% do some stuff
% add (x,y) to clusterPoints
clusterPoints = [clusterPoints [x;y] ];
end
In the above, we assume that each point is 2D only. Once we break out of the while loop, clusterPoints will be a 2xm vector where m is the number of points that we have added to the array. The first row will correspond to all x's, and the second row will correspond to all y's.

5 件のコメント

Hello Geoff,
The case is I want to assign a group of data points to a new array.
% f is an original dataset.
% A is an array that result from clustering function, in my case, I
have multiple arrays of (A) that I need to store them in separate
arrays to use them in a single plot.
while true
[A] = clustring(f);
% I want to store the array A in another array before move to another
iteration
end
I wish it is clear enough now!
Thank you Geoff
Geoff Hayes
Geoff Hayes 2014 年 11 月 1 日
Hi Abdulatif - try the same above, but use a cell array to capture the the clustered data at each iteration
% initialize the cluster points array to be empty
clusterPoints = {};
atCluster = 0;
while true
% do some stuff
atCluster = atCluster + 1;
clusterPoints{atCluster} = clustring(f);
end
You could use the same concatenation as the previous example (using the []), but this is just an alternative.
Abdulatif Alabdulatif
Abdulatif Alabdulatif 2014 年 11 月 2 日
編集済み: Abdulatif Alabdulatif 2014 年 11 月 2 日
Hi Geoff,
It is work. However I do not know how I can access to data and use them in plot.
clusterPoints contains: [58x3 double] [32x3 double] [1x3 double] []
How can I use them as numerical data? (Cell array concept is new to me!) : )
Thanks for corporation Geoff
Geoff Hayes
Geoff Hayes 2014 年 11 月 2 日
If you wish to plot the data by cluster, then you could try
figure;
hold all; % use hold all to plot clusters in different colours
for k=1:length(clusterPoints)
data = clusterPoints{k};
if ~isempty(data)
plot3(data(:,1),data(:,2),data(:,3),'o');
end
end
Abdulatif Alabdulatif
Abdulatif Alabdulatif 2014 年 11 月 3 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by