Image analysis program: 'find' not working

testnumber=2;
vectortofindtestnumber=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 19 20
];
checkiftestelementhasduplicatesinthevector=find(testnumber==vectortofindtestnumber);
if(length(checkiftestelementhasduplicatesinthevector)>1)
disp('Found a duplicate'); % Do some steps
end
for '2' my code in my image analysis program is going into if condition and is getting processed.
I feel definitely this is a run-time bug, but any similar experiences any of you can share would be helpful.
Any help would be appreciated.
Thanks in advance

回答(2 个)

You do not show any "else" after the length check, and your "Do some steps" does not show any action to avoid doing the code that is after the "if". Remember, if you have an "if" then that does not mean skip all the rest of the program if the condition is false: it means that if the condition is false, skip only what is in the "if" block.

5 个评论

Hi Walter Roberson Thanks for the reply and your time. I am still not able to figure out, what's the problem. My code in more detail:
vectortocompare=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
for checkeachelem=1:length(vectortocompare)
duplicatesinfo=find(vectortocompare(checkeachlelem)==vectortofindtestnumber);
if(length(duplicatesinfo)>1)
disp('Duplicates found');
disp(duplicatesinfo);
else
disp('No duplicates');
end
end
Caution: you have vectortocompare(checkeachlelem) with checkeachlelem being a misspelling.
We do not have your vectortofindtestnumber so we cannot test the code.
What output are you observing?
Forgive my typos in the previous post.
My code:
vectortofindtestnumber=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 19 20
];
vectortocompare=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
for checkeachelem=1:length(vectortocompare)
duplicatesinfo=find(vectortocompare(checkeachelem)==vectortofindtestnumber);
if(length(duplicatesinfo)>1)
disp('Duplicates found');
disp(duplicatesinfo);
else
disp('No duplicates');
end
end
From command line it is giving me the expected result, my duplicates (repeated numbers in vectortofindtestnumber) are 19 and 21 i.e., value 19 in vectortofindtestnumber and 20 and 22 i.e., value 20 in the same vector.
But, while executing the program, value 2 seems to be identified as a duplicate (repeated number in the above mentioned vector), which I don't understand.
Thanks once again for your time!
When you say "from command line" compared to "while executing the program", what do we need to know about how you are executing the program? Did you write it to a .m file and are giving the name of the .m file? If so then have you checked with "which" to be sure you are executing the correct .m file ? Did you add anything else to that file?
Hi The demarcation I made 'from command line' and 'while executing the program' is premature on my part..to say..now that I figured out that the variables on which my program is working have different data to what I expected. It's my logical misunderstanding, apologies for the waste of time.
Why not simply use ismember:
% Define the large list to search.
vectorToCompare=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
% Define the numbers to look for in the larger list.
testnumber= [2 2 5 42];
[hits, indexes] = ismember(testnumber, vectorToCompare)
if any(hits)
message = sprintf('%d numbers found.', sum(hits));
else
message = sprtinf('No numbers found.');
end
msgbox(message);
P.S. How does this have to do with image analysis?

3 个评论

Thank you! Will try this and get back to you! I like the way you answer questions, and wanted to make sure my question doesn't escape from your notice! :) Thanks again!
Did it work? It worked for me.
Hi
Yes, it did work. But I am afraid that the data structure (vector in the program mentioned) my program is working on is quite different. The error I quoted is a result of my logical gap in understanding what my program is processing. Thank you

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by