MATLAB Answers

how to use for loop to solve this problem

6 views (last 30 days)
if i have two function
the first function use variables "p" and "r" and result s as follow
function [label,s] = LSC(data,k)
if (~exist('opts','var'))
opts = [];
end
N=5;
p =20;
r = 10;
maxIter = 100;
numRep = 10;
mode = 'kmeans';
nSmp=size(data,1);
% Landmark selection
if strcmp(mode,'kmeans')
kmMaxIter = 5;
if isfield(opts,'kmMaxIter')
kmMaxIter = opts.kmMaxIter;
end
kmNumRep = 1;
if isfield(opts,'kmNumRep')
kmNumRep = opts.kmNumRep;
end
[dump,marks]=litekmeans(data,p,'MaxIter',kmMaxIter,'Replicates',kmNumRep);
clear kmMaxIter kmNumRep
elseif strcmp(mode,'random')
indSmp = randperm(nSmp);
marks = data(indSmp(1:p),:);
clear indSmp
else
error('mode does not support!');
end
.
.
[label,s] = kmedo(U',k);
end
the second function is
function Accuracy=yarbb(data,x)
for i=1:100
rng('default')
*[cluster_labels,s] = LSC(adj,x);*
coordinates=data;
Av=[coordinates; fliplr(coordinates)];
linindices = sub2ind(size(s), Av(:, 1), Av(:, 2))';
remain=setdiff(1:numel(s), linindices);
sim=s(remain);
similarity=unique(sim);
.
.
.
auc= (ndash + 0.5 * nddash)/(ndash+nddash+nn);
end
Accuracy = mean(auc)
we note that the second function call the first function in line 4,so i want to variables "p" ,"r" in first function to take values 2:10 and each time calculate accuracy(second function)i.e calculate first function at p=2,r=2 then calculate second function then in second time p=3,r=2 and calculate second function and so on.

Accepted Answer

Frank Macias-Escriva
Frank Macias-Escriva on 18 Feb 2017
First, you have to pass "p" and "r" as arguments to your function LSC. If you need backward compatibility with old calls that don't include those arguments you can implement "default values" for arguments using nargin. (A bad practice but another way is using "p" and "r" as global variables). So your first function will start:
function [label,s] = LSC(data, k, p, r)
I guess you mean that you want to pass a different pair of "p" and "r" for each of the 100 steps in the "for" loop. In that case, "p" and "r" should go from 1:100, if they go from 2:10 (9 values each) you will end up with 81 combinations (pairs), not 100. Based on that assumption, the solution would be to generate the corresponding values for "p" and "r" just before calling LSC. In line 4 of your second function, before calling LSC, you should add:
r = floor((i-1)/10) + 1;
p = mod(i-1,10) + 1;
Then call your function:
[cluster_labels,s] = LSC(adj, x, p, r);
If before changing your code you want to give a taste of what your "p" and "r" values will be just run the following code:
for i=1:100
disp(['Step ' num2str(i) ': r=' num2str(floor((i-1)/10)+1) ', p=' num2str(mod(i-1,10)+1)]);
end;
Hope this help you.
Best,
fm

More Answers (0)

Community Treasure Hunt

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

Start Hunting!