Compare Two Matrices

This is a bit of code I quickly wrote to compare the equivalent i,j values in two same size matrices and tell if they are equal or not. There are probably easier ways to do this, such as subtract one from the other and display the result (all 0's would mean equal), or use the matrixop equal(a,b) operation. But I added an extra bit of code to set the level of similarity, a maximum difference between the two values, the variable  closeness . This code could easily be modified to compare two 1D, 3D, etc. waves as well. The function creates and displays a matrix the same size as the input matrices with ones and zeros as values.

Function matrixcomp(w1,w2,closeness)
    WAVE w1        
    WAVE w2
    Variable closeness
       
    variable a = dimsize(w1,0)
    variable b = dimsize(w1,1)
    variable c = dimsize(w2,0)
    variable d = dimsize(w2,1)
   
    if(a != c || b != d)
        abort "Same size matrices please!"
    endif
   
    redimension/N=(a*b) w1
    redimension/N=(a*b) w2
   
    variable i=0
    make/O/N=(a*b) CompMat
   
    Do
        if(abs(w1[i]-w2[i]) <= closeness)
            CompMat[i] = 1
        else
            CompMat[i] = 0
        endif
        i+=1
    While(i<(a*b))
   
    redimension/N=(a,b) w1
    redimension/N=(c,d) w2
    redimension/N=(a,b) CompMat
    display; appendimage CompMat
End

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More