I want all pairwise comparisons for all rows in the matrix, obviously double for loop will work but extremely expensive for large dataset.
I looked up implicit loop like apply(), etc. but have no a clue how to avoid the inner loop.
How can it be achieved?
I’m assuming you’re trying do some type of comparison across all row-pairs of a matrix.
You could use
outer()to run through all pairs of row-indices, and apply a vectorizedcomparison function to each row-pair. E.g. you could calculate the squared Euclidean distance among all row-pairs as follows: