From: Jan on
Hi,

Is there a way to automatically genereate new variables with a specified name?
I first will explain my problem: I use fuzzy-c clustering as segmentation algorithm, and it works well. But I want to give a number of clusters from my GUI and then automatically segmentate in stead of making the variables by myself.

I have this:
[ClusterCenters, MembershipVal, obj_fcn] = fcm(imROI, numClusters);

maxMem = max(MembershipVal);

% Search index of maxMembershipVal
index1 = find(MembershipVal(1,:) == maxMem);
index2 = find(MembershipVal(2,:) == maxMem);
index3 = find(MembershipVal(3,:) == maxMem);

Now I want to make the index part automatically because if I have 3 clusters, it looks like the above part, but when I have 7 clusters, I need 7 indexes.
Now I tried the code below, but I doesn't work because the number of values per index can change.

index = zeros(numClusters, SizeImage(1)*SizeImage(2));
for i = 1:numClusters
expres = strcat('find(MembershipVal(',num2str(i),',:) == maxMem)');
index(i,:) = eval(expres);
end

fcmImage(1:length(imageROI)) = 0;

Can somebody help me, or has an other solution to this problem?
Thanks in advance!
From: Matt Fig on
Don't do it!


http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
From: Walter Roberson on
Jan wrote:

> Is there a way to automatically genereate new variables with a specified
> name?

> Now I want to make the index part automatically because if I have 3
> clusters, it looks like the above part, but when I have 7 clusters, I
> need 7 indexes.
> Now I tried the code below, but I doesn't work because the number of
> values per index can change.
>
> index = zeros(numClusters, SizeImage(1)*SizeImage(2));
> for i = 1:numClusters
> expres = strcat('find(MembershipVal(',num2str(i),',:) == maxMem)');
> index(i,:) = eval(expres);
> end


Why not use

index = cell(numClusters,1);
for xi = 1 : numClusters
index{i} = find(MembershipVal(i,:) == maxMem);
end

The eval() is unneeded, and using a cell array instead of a numeric array
solves the problem of results of differing length.
From: Jan on
Thanks for your reply's it works perfect!!