# Matrix comparison

Hi everyone,
I just wanted to compare 2 matrices for equal content and I get a result I don't understand:

`testMX_001 = Matrix( 5, 5, 7 );
testMX_002 = Matrix( 5, 5, 8 );

equal = False;

if( testMX_001 == testMX_002 )
{
equal = True;
}

equal = False;

if( testMX_001 != testMX_002 )
{
equal = True;
}`

here both if clauses succeed, so how can == and != be the same result ? what does the operator compare inside the matrices ? Thank you for any answer

Comparison operator applied on Matrices returns a MATRIX because it operates item-wise (item by item) and result of comparison is the MATRIX that gives 1 when given (n,k) item is the same or 0 otherwise.

``````// comparison of matrices results in a matrix!
item_wise_equality = testMX_001 == testMX_002;

// comparison of matrices results in a matrix!
item_wise_inequality = testMX_001 != testMX_002;

printf("%s", MxToString( item_wise_equality ) );
printf("%s", MxToString( item_wise_inequality ) );
``````

Then if() clause applied on entire matrix just checks if matrix exists (and it is not Null).

Both "item by item" equality and inequality matrices EXIST (and they are not Null). Note that Null in AFL is not zero.

Use the DEBUGGER you would see the values

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

You could use the code like this to do what you wanted:

``````if( MxSum( testMX_001 != testMX_002 ) == 0 )
{
equal = True;
}
``````

This will work because MxSum returns a scalar value representing sum of all matrix items. If the above sum is zero it means that inequality check for all elements was false, so all elements have equal value.

When posting the formula, please make sure that you use Code Tags (using `</>` code button) as explained here: How to use this site. Code tags are required so formulas can be properly displayed and copied without errors.

Ah Ok. I like the MxSum solution (: thank you. P.S. I didn't use the code button on a new line, that seems to make a difference, sorry.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.