Comparing between Array Elements

358 ビュー (過去 30 日間)
tarmizi adam
tarmizi adam 2011 年 11 月 24 日
Hi all, i've been trying to do some kind of comparison between two array
Basically it works like this:
The first array called "Answer" has 1 x 416 double elements in it.
The second is an array called "Target" which has 1x 260 double elements in it.
What i want to do is;
1) compare each element in "Answer" with "Target"
2) If the value is the same between the element in "Answer" and "Target"
3) increment a variable called 'Match' by 1
4) find the percentage of the Matched elements. This is done by the formula: (match/lenght(Answer))* 100
This is the code that i did:
L = length(Yt); %number of test pattern
ans = 1;
for m = 1:L
max = 0.0;
for n = 1:26
if (Yt(n,m)>= max)
max = Yt(n,m);
ans = n;
Answer(m) = ans;
fprintf(F1,'Pattern %d => %d ',m,ans);
match = 0;
for i = 1:260
for k = 1:L
if (Answer(k) == Target(i))
match = match+1;
RR = (match / L)*100;
fprintf(F1,'\ntraining time : %.4f min =%.2f hours \n',mintime,hrtime);
fprintf(F1,'\n recognition rate : %.2f \n',RR);
---------------------------------------------------------- Here, 'Yt' 26x416 matrix.Thus, L = 416; the output of the recognition rate gives me 992.84, which is obviously not a percentage value. (99.284% would be logical). Do you have any suggestion ? thanks...

回答 (4 件)

Walter Roberson
Walter Roberson 2011 年 11 月 24 日
percentage = mean(ismember(Answer, Target)) * 100;

Sven 2011 年 11 月 24 日
If you were running the code you put in the question, you should have gotten an error (rather than an absurd answer). There was a typo in the line:
for 1 = 1:260
which should have read:
for i = 1:260
When I change that and run, I get a reasonable number:
Answer = randi(1000, 1, 416);
Target = randi(1000, 1, 260);
match = 0;
for i = 1:260
for k = 1: length(Answer)
if (Answer(k) == Target(i))
match = match + 1;
percentage = (match/length(Answer))*100;
It tells me that "percentage" is around 24. Is that reasonable?
Note that you can replace your loop entirely:
percentage = 100 * nnz(ismember(Answer, Target)) / length(Answer);
Now we should be clear about something. My one-line command asks "what percentage of the elements in Answer also appear in Target?"
This is slightly different to your original loop, because your original loop will add a new match for every time an element of Answer appears in Target. So if you had:
Answer = [10 20 30 40];
Target = [20 20 20 20 20 20];
Then my one-liner will return 25% (because 1 in 4 elements match), but your loop will return 150% (because 1 in 4 elements match, but they match 6 times).
Did that answer the question?
  2 件のコメント
Sven 2011 年 12 月 29 日
Tarmizi, can you explain how the answers given here don't answer your question? Each answer given showed how you can remove your loop and replace it with a simpler 1-line piece of code, but your comment here still has the same loop as in your original question.
Furthermore, your updated question still cannot be run by anybody because it uses variables that you never create in your code, so we can't actually reproduce the strange output you say you are getting.


Image Analyst
Image Analyst 2011 年 11 月 24 日
You probably failed to consider the FAQ when comparing double class variables for equality.
You should check for tolerance instead of equality:
if abs(Answer(k) - Target(i)) < 0.001
or something similar.
for 1 = 1:260
won't work. In fact I'm surprised you don't get a syntax error - you should have. Try changing the one to kk and the i in the loop also to kk, because you have no loop iterator and i would be a bad one to choose anyway since it's the imaginary constant.

LY Cao
LY Cao 2011 年 11 月 24 日
this code can replace your original loop:
c = bsxfun(@eq,Answer,Target.');
percentage = nnz(c)/length(Answer)*100;
'ismember' is slightly different to your original loop.


Community Treasure Hunt

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

Start Hunting!

Translated by