How to solve the error messege in matlab?

I always get this error messege when i run this code in matlab. I want to do template matching to recognise the character.
??? Operands to the || and && operators must be convertible to logical
scalar values.
Error in ==> readLetter at 17
if vd==1 || vd==2
The code is :
load NewTemplates % Loads the templates of characters in the memory.
gambar=imresize(gambar,[42 24]); % Resize the input image so it can be
compared with the template's images.
comp=[ ];
for n=1:length(NewTemplates)
sem=corr2(NewTemplates{1,n},gambar); % Correlation the input image with
every image in the template for best matching.
comp=[comp sem]; % Record the value of correlation for each template's
character.
end
vd=find(comp==max(comp)); % Find the index which correspond to the highest
matched character.
%*-*-*-*-*-*-*-*-*-*-*-*-*-
% Accodrding to the index assign to 'letter'.
% Alphabets listings.
if vd==1 || vd==2
letter='A';
How to solve it?

Answers (2)

In Matlab windows command type
size(vd)
You will see that the length of vd is >1
How can you compare [1 2] with 2 for example?
vd=find(comp==max(comp));
if vd==1 || vd==2
As the error says "||" is the short-circuiting version of "|" and can only be used on single-valued expressions.
In you code find can return a vector of length>1 if there's more than one value in the test that is true. Whether this is what you want or not you'll have to decide based on what you're actually trying to do. As written it finds all locations where the max value exists. If you want only one , then use the optional argument to specify that. See
doc find % for more detail
Failing that, the short answer to make the error go away is to replace w/ |. Of course, then you'll get the problem that the 'if' will never be true because in Matlab if is true iff all elements in the array are T.
So, while it's not totally clear what you're doing from what is shown, in all likelihood you want
vd=find(comp==max(comp),1);
if vd==1 | vd==2
but that's supposing a fair amount. If you do need to do something w/ all locations that match the max() value then you'll have to write code to process the full length of the returned vd vector.
If that's not enough to figure out what your real solution is to accomplish the object, amplify the information on what you're trying to do.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 4 Aug 2013

Community Treasure Hunt

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

Start Hunting!