Greetings.

I didn't find any built-in function to return the indices for the unique 
rows in a matrix, so I tried rolling my own.  I looked at, but didn't 
understand, the highly macro-ized, arbitrary dimensional code in the 
built-in unique() function.  I did discover hashing, though, from looking 
at that code.  Here is what I have so far:

 function uniquerowindices2{T}(a::Matrix{T})
     rowhash = Uint64[hash(a[k,:]) for k = 1:size(a,1)]
     hu = unique(rowhash)
     ind = zeros(Int,length(hu))
     k1 = 1
     for m = 1:length(ind)
         for k = k1:length(rowhash)
             if rowhash[k] == hu[m]
                 ind[m] = k
                 k1 = k
                 break
             end
         end
     end
     return ind
 end

This is quite a bit faster than my first effort, where I was comparing 
rows, rather than row hashes, but I'm betting someone out there has a much 
better way.  Thanks in advance,

--Peter    

Reply via email to