Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Problem with for loop

1 view (last 30 days)
siti nur
siti nur on 3 Apr 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
I'a having a problem with the loop.I am doing face recognition in ANFIS. I am using 3 statistical features for 1 image.I already train and test the network.Output for the network in 1 image is:
output=0.0014
0.2992
-0.8178
then, i have to subtract the output with each value in the database so that when the answer is [0.00 0.00 0.00]', it's mean i already find the exact match.
The database is:
database=0.0014 0.5208 -0.0000
0.2992 0.2358 -0.0003
-0.8178 0.4429 -0.0077
0.0018 -0.0000 0.0008
-0.0000 -0.0000 0.0131
0.0000 0.0005 0.4585
-0.0020 0.9925 1.0074
-0.0001 0.9557 0.9999
-0.0035 0.6546 0.9882
1.0399 0.9322 0.5067
0.6241 0.9999 1.0000
0.5647 1.0175 0.9999
0.9990 1.0006
1.1715 0.9999
0.9075 0.9778
i am using for loop to do the process:
[l,m]=size(database);
for i=1:1:m
for j=1:1:l
diff=anfis_eval(j,i)-anfis_eval1(j,1);
if(diff==0)
ans=i:
end
end
end
I manage to get the exact match, my problem now is that what if there is no exact
match and I want to find the smallest different.I think i should put the 'else'
condition so that if the answer is not zero than I shoul go to another condition.
[l,m]=size(database);
for i=1:1:m
for j=1:1:l
diff=anfis_eval(j,i)-anfis_eval1(j,1);
if(diff==0)
ans=i:
else
?????
end end end
Now, I don't know how to find the smallest different.Can somebody help me?

Answers (1)

Jarrod Rivituso
Jarrod Rivituso on 3 Apr 2011
Forgive me if you have already thought of this, but couldn't you just keep track of the smallest answer found? Something like...
[l,m]=size(database);
smallestDiff = 1e6; %or some other very high value
for i=1:1:m
for j=1:1:l
diff=anfis_eval(j,i)-anfis_eval1(j,1);
if(abs(diff) < smallestDiff)
result=i;
smallestDiff = abs(diff);
end
end
end
Also, I would avoid using ans as a variable name, since on the command prompt, MATLAB will sometimes use that variable name as the default return argument to function calls.

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!