Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compare two arrays

Subject: Compare two arrays

From: sam

Date: 14 Jan, 2013 04:35:12

Message: 1 of 4

Hello every one ..

I write a simple program to compare between two binary arrays, when the arrays is the same values the program work in good way, but when there is some errors in any arrays the program does not work correctly.
 below the program that I wrote .. where is the problem (the error) in this program ?
================================
Y1=[1 0 1 1];

Y2=[1 0 1 0];

x1=[1 0 0 1];

x2=[1 0 1 0];

if ((Y1 == x1) & (Y2 == x2))
    
 disp ('Y1 = X1 AND Y2 = X2')
 
elseif((Y1 ~= x1) & (Y2 ~= x2))
    TNOBE=TNOBE+2
    disp ('There is Error in X1 and X2')
elseif((Y1 ~= x1) & (Y2 == x2))
    TNOBE=TNOBE+1
     disp ('There is Error in X1')
elseif((Y1 == x1) & (Y2 ~= x2))
    TNOBE=TNOBE+1
     disp ('There is Error in X2')
else
    
    disp ('The program not work' )
end
=================================

Thank you with best regards

Sam
 

Subject: Compare two arrays

From: Roger Stafford

Date: 14 Jan, 2013 05:11:11

Message: 2 of 4

"sam " <s_abcdf@yahoo.com> wrote in message <kd01u0$47g$1@newscl01ah.mathworks.com>...
> I write a simple program to compare between two binary arrays, when the arrays is the same values the program work in good way, but when there is some errors in any arrays the program does not work correctly.
> ...........
> if ((Y1 == x1) & (Y2 == x2))
> disp ('Y1 = X1 AND Y2 = X2')
> elseif((Y1 ~= x1) & (Y2 ~= x2))
> TNOBE=TNOBE+2
> disp ('There is Error in X1 and X2')
> elseif((Y1 ~= x1) & (Y2 == x2))
> TNOBE=TNOBE+1
> disp ('There is Error in X1')
> elseif((Y1 == x1) & (Y2 ~= x2))
> TNOBE=TNOBE+1
> disp ('There is Error in X2')
> else
> disp ('The program not work' )
> end
- - - - - - - - -
  It is true that both the 'if' and 'elseif' statements require all elements of a nonscalar logical expression to be true. This does accomplish what you want at the first step, but not in the following steps. You don't want to exhibit 'There is Error in X1 and X2' only when each element of X1 differs from the corresponding element of Y1 and each element of X2 differs from each one of Y2. You want it exhibited when there are any differences even as few as one in either X1 or X2. For this purpose you should be writing something like this.

 t1 = all(X1==Y2);
 t2 = all(X2==Y2);
 if t1 & t2
  (both okay)
 elseif ~t1 & ~t2
  (both in error)
 elseif ~t1
  (X1 in error)
 else
  (X2 in error)
 end

Roger Stafford

Subject: Compare two arrays

From: Matt J

Date: 14 Jan, 2013 05:23:12

Message: 3 of 4

"Roger Stafford" wrote in message <kd041f$b2v$1@newscl01ah.mathworks.com>...
> "sam " <s_abcdf@yahoo.com> wrote in message <kd01u0$47g$1@newscl01ah.mathworks.com>...
> > I write a simple program to compare between two binary arrays, when the arrays is the same values the program work in good way, but when there is some errors in any arrays the program does not work correctly.
>
> It is true that both the 'if' and 'elseif' statements require all elements of a nonscalar logical expression to be true. This does accomplish what you want at the first step, but not in the following steps. You don't want to exhibit 'There is Error in X1 and X2' only when each element of X1 differs from the corresponding element of Y1 and each element of X2 differs from each one of Y2. You want it exhibited when there are any differences even as few as one in either X1 or X2. For this purpose you should be writing something like this.
>
> t1 = all(X1==Y2);
> t2 = all(X2==Y2);
===============

Or,

t1=isequal(X1,Y1);
t2=isequal(X2,Y2);

etc...

Subject: Compare two arrays

From: sam

Date: 14 Jan, 2013 06:09:11

Message: 4 of 4

"Roger Stafford" wrote in message <kd041f$b2v$1@newscl01ah.mathworks.com>...
> "sam " <s_abcdf@yahoo.com> wrote in message <kd01u0$47g$1@newscl01ah.mathworks.com>...
> > I write a simple program to compare between two binary arrays, when the arrays is the same values the program work in good way, but when there is some errors in any arrays the program does not work correctly.
> > ...........
> > if ((Y1 == x1) & (Y2 == x2))
> > disp ('Y1 = X1 AND Y2 = X2')
> > elseif((Y1 ~= x1) & (Y2 ~= x2))
> > TNOBE=TNOBE+2
> > disp ('There is Error in X1 and X2')
> > elseif((Y1 ~= x1) & (Y2 == x2))
> > TNOBE=TNOBE+1
> > disp ('There is Error in X1')
> > elseif((Y1 == x1) & (Y2 ~= x2))
> > TNOBE=TNOBE+1
> > disp ('There is Error in X2')
> > else
> > disp ('The program not work' )
> > end
> - - - - - - - - -
> It is true that both the 'if' and 'elseif' statements require all elements of a nonscalar logical expression to be true. This does accomplish what you want at the first step, but not in the following steps. You don't want to exhibit 'There is Error in X1 and X2' only when each element of X1 differs from the corresponding element of Y1 and each element of X2 differs from each one of Y2. You want it exhibited when there are any differences even as few as one in either X1 or X2. For this purpose you should be writing something like this.
>
> t1 = all(X1==Y2);
> t2 = all(X2==Y2);
> if t1 & t2
> (both okay)
> elseif ~t1 & ~t2
> (both in error)
> elseif ~t1
> (X1 in error)
> else
> (X2 in error)
> end
>
> Roger Stafford


Thank you Roger Stafford with best regards

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us