# Clustering Matrices

Suppose I have a set of 100 $n \times 2$ matrices that all have the following format:

Bid    Profit
[5.00   7.10]
[3.14   6.04]
[2.9    10.08]


Where the numbers are sample values. Now I want to cluster these matrices together using R or some statistical software. How can I do this? I've looked into hclust and time series clustering in R but I don't think this is what I want.

## Edit

In the attached picture, I have three sample matrices plotted. For simplicity's sake I have plotted them all with the same x-values but this may not necessarily be the case. I want a way of saying "at points 3,8,9 (the three highest points on the black curve), this black curve is similar to the blue curve but not the red curve". Ordering of the rows of the matrix should not matter - I can just sort by the first column to ensure that the x-values (Bid) are ordered least to greatest.

## edit 2

These graphs are included as if the first column in all three matrices was the same i.e., all three matrices had the form

xval yval
[a x1]
[b x2]
[c x3]
[d x4]
...


and then the matrices are plotted with the first column on the x-axis and the second column on the y-axis.

• Perhaps I should unwrap these matrices into a vector and use something like this package? jstatsoft.org/v62/i01/paper Mar 23 '15 at 19:56
• What I meant by stacking is if you have 100 3x2 matrices could you make this into one 300x2 matrix. Perhaps the question I should've asked is, what do the rows represent? Mar 23 '15 at 20:52
• What exactly do you mean by "clustering?" Can you be more specific on what it is you are trying to accomplish? Mar 23 '15 at 21:00
• Well there are plenty of matrix norms, and where there are norms there are distances (take the difference). But you want these distances to make sense for your problem. So what kind of similarity are you interested in? If I multiplied all of the elements by 2, should that be similar? Or quite different. What if I reordered the rows?... Mar 24 '15 at 1:07
• Mike, these graphs have confused me a lot more. In what way are they plots of 3x2 matrices? Mar 24 '15 at 14:57

If you needed to cluster whole matrices you would need a special distance measure for matrices. Fortunately, your situation appears to be much simpler than that. Given that you are only interested in the third, eighth, and ninth points / rows in each matrix, I would start by throwing all other rows away. Next, given that these three points will always have identical xval values, you can throw the xval column away (assuming the rows are always in the same order). You now have 100 vectors of length 3.
Those can be formed into a new, single matrix by transposing them (into row vectors) and stacking them together. That is, the first row in the new matrix will be the yvals from the first of the original set of matrices, the second in row two, etc. In the first column, all the way down, will be the numbers that had corresponded to the third point, the second column the eighth, and the last column the ninth. Now you have a single matrix with patters to be clustered in rows and the dimensions / feature space within which they will be clustered in the columns. This is just like any ordinary cluster analysis.
• So you are saying that if I only care about the 3,8,9 row, then I can cluster so that each row is treated as a distinct observation? So I can get a measure of how distant each row is from each other? If you could point me toward some R functions that would be very helpful. Thanks for the answer! Mar 24 '15 at 21:47
• @Mike, R functions in what sense? There is dist(), hclust(), kmeans(), etc. For the data management, that's really a Stack Overflow question, but you'd just make an empty matrix m = matrix(NA, nrow=100, ncol=3), & if your set of original matrices were in a list l, you could fill your new matrix w/ a loop for(i in 1:100){ m[i,] = l[[i]][c(3,8,9), 2] }. Mar 24 '15 at 21:56