Is there a way to calculate the following specified matrix by avoiding loops? in R or Matlab

走远了吗. 提交于 2020-01-05 10:31:56

问题


I have an N-by-M matrix X, and I need to calculate an N-by-N matrix Y:

Y[i, j] = sum((X[i,] - X[j,]) ^ 2)   0 <= i,j <= N

For now, I have to use nested loops to do it with O(n2). I would like to know if there's a better way, like using matrix operations.

more generally, sum(....) can be a function, fun(x1,x 2) of which x1, x2 are M-by-1 vectors.


回答1:


you can use expand.grid to get a data.frame of possible pairs:

X <- matrix(sample(1:5, 50, replace=TRUE), nrow=10)

row.ind <- expand.grid(1:dim(X)[1], 1:dim(X)[2])

Then apply along each pair using a function:

myfun <- function(n) {
  sum((X[row.ind[n, 1],] - X[row.ind[n, 2],])^2)
}

Y <- matrix(unlist(lapply(1:nrow(row.ind), myfun)), byrow=TRUE, nrow=nrow(X))


> Y
      [,1] [,2] [,3] [,4] [,5]
 [1,]    0   28   15   31   41
 [2,]   31   28   33   30   33
 [3,]   28    0   15    7   19
 [4,]   33   30   19   34   11
 [5,]   15   15    0   12   22
 [6,]   10   19   10   21   20
 [7,]   31    7   12    0    4
 [8,]   16   17   16   13    2
 [9,]   41   19   22    4    0
[10,]   14   11   28    9    2
> 

I bet there is a better way but its Friday and I'm tired!




回答2:


(x[i]-x[j])^2 = x[i]² - 2*x[i]*x[j] + x[j]²

and than is middle part just matrix multiplication -2*X*tran(X) (matrix) and other parts are just vetrors and you have to run this over each element

This has O(n^2.7) or whatever matrix multiplication complexity is

Pseudocode:

vec=sum(X,rows).^2
Y=X * tran(X) * -2 
for index [i,j] in Y:
   Y[i,j] =  Y[i,j] + vec[i]+vec[y]



回答3:


In MATLAB, for your specific f, you could just do this:

Y = pdist(X).^2;

For a non-"cheating" version, try something like this (MATLAB):

[N, M] = size(X);
f = @(u, v) sum((u-v).^2);
helpf = @(i, j) f(X(i, :), X(j, :))
Y = arrayfun(helpf, meshgrid(1:N, 1:N), meshgrid(1:N, 1:N)');

There are more efficient ways of doing it with the specific function sum(...) but your question said you wanted a general way for a general function f. In general this operation will be O(n^2) times the complexity of each vector pair operation because that's how many operations need to be done. If f is of a special form, some calculations' results can be reused.



来源:https://stackoverflow.com/questions/11059274/is-there-a-way-to-calculate-the-following-specified-matrix-by-avoiding-loops-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!