Every time I run the code, value of (k) changes. why?

function [label] = clutseringData (Data , C)
dis = zeros(size(Data,1), size(C,1));
label = zeros(size(Data,1), 1);
for i = 1:size(Data,1)
for j = 1:size(C,1)
dis(i, j) = norm(Data(i,:) - C(j, :));
end
end
for k = 1:size(Data,1)
label(k) = find( dis(k,:) == min(dis(k,:)) );
end
end

5 Comments

What are the values of input argument Data and C? What do you mean that value of k changes? It is a counter variable for a for-loop, therefore it will change from 1 to size(Data,1).
I'm trying to build a kohonen algorithm so, Data is the training data of the model, its size is 300*2:
trainingData = [randn(100,2)*100+ones(100,2); randn(100,2)*0.5-ones(100,2); randn(100,2)*0.75];
C is the weights after making the algorithm calculation which it 4*2 matrix so I'm trying to pass those vaules to the clutseringData function
label = clutseringData (trainingData , c);
So, k = 1:300; is equavilant to k = 1:size(Data,1)
And I mean that each time I click on run, k becomes 2 , 11, 175 or any different value and sometimes 300 in the workspace. I know it must iterate 300 time "from 1 to size(Data,1)", but its value is not 300 to make the right iteration it's dynamically changes !!
I'm not so sure about that, but for my understanding, for the case your program runs without error, k shouldn't be in the workspace at all, since it's not an output variable. So I would suspect, that the k that you see in you workspace could be from former tests, where you ight have set some breakpoint or something like that. I don't now---the length of label is correct?
This is my original Mian script, the previous function I made it trying to avoid that changing in k value, then k shows in the workspace before the function creation and still changing everytime I run the code showing that error
(In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Main (line 27)
label(k) = find( dis(k,:) == min(dis(k,:)) );)
and I think the length of label is correct ..
clc , clear , close all;
networkWidth =4;
networkHight = 4;
iterations = 300;
intialLearningRate = 0.1;
intialRedius = 1 ;
plotMute = 0;
trainingData = [randn(100,2)*100+ones(100,2); randn(100,2)*0.5-ones(100,2); randn(100,2)*0.75];
trainingData = featureNormalize(trainingData);
c = som (trainingData, networkWidth, networkHight, iterations...
, intialLearningRate,intialRedius, plotMute);
%label = clutseringData (trainingData , c);
dis = zeros(size(trainingData,1), size(c,1));
label = zeros(size(trainingData,1), 1);
for i = 1:size(trainingData,1)
for j = 1:size(c,1)
dis(i, j) = norm(trainingData(i,:) - c(j, :));
end
end
for k = 1:size(trainingData,1)
label(k) = find( dis(k,:) == min(dis(k,:)) );
end
Menna, I guess that you are trying to find the index of the minimum value in the dis. Right? What if there are two equal minimum values.

Sign in to comment.

 Accepted Answer

If you are trying to find the index of minimum value, then the following use following statement
[~,label(k)] = min(dis(k,:));
it returns the index of the minimum value in k-th row. If two minimum values are equal, then it returns the index of the first value.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!