Skip to content

logical comparison of two matrices

2 messages · Brian Ripley, Federico Calboli

#
Use all() or any() to reduce your comparison to a single logical value.
In your case all(S == P) appears to be what you intended, although maybe 
not what you wanted (see the next para).

Also check out ?identical and the comments in ?"==" (which BTW contains 
the answer to your question).
On 24 Jan 2004, Federico Calboli wrote:

            

  
    
#
Dear All,

how can I get a logical comparison between matrices (or vectors) in a if
statement?

Whenever I try I get the following:
+ if (S == P){
+ return("OK")
+ }
+ else {
+ return("No match")
+ }
+ }
[1] "No match"
Warning message:
the condition has length > 1 and only the first element will be used in:
if (S == P) {

The warning clearly states that only the first element was used, and
this would not be good enough.

If comparing the whole matrices is not possible I could be happy just
comparing the two diagonals.

regards,

Federico Calboli