如何在 matlab 上重现用于计算 pagerank 转换矩阵的数学代码?

How do I reproduce this mathematica code for calculating the transition matrix of pagerank on matlab?

所以需要执行的公式为:

P = ((1 - delta)/n) + ((delta)*(A)ij / (Sigma(k=1 to n)(A)ik))

where delta = 0.85
n = 8
and A = the adjacency matrix for web pages being surfed

The mathematica code for it is:

    A = {{1, 1, 1, 0, 0, 1, 0, 1}, {0, 0, 1, 0, 0, 1, 0, 1}, {1, 1, 0, 1, 
        0, 1, 1, 0}, {0, 1, 1, 0, 1, 0, 1, 0}, {1, 1, 1, 1, 0, 1, 1, 
        1}, {1, 1, 1, 0, 0, 1, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {0, 0, 0, 
        0, 1, 0, 0, 1}};

       n = 8;

       \[Delta] = 0.85;

       P = Transpose[Table[(1 - \[Delta])/n + (\[Delta]*A[[i, j]])/(Sum[A[[i, k]], {k, 1, n}]), {i, 1, n}, {j, 1, n}]];

其他一切都只是插入数字。 现在我遇到的主要问题似乎是让 A[[i,j]]/Sum[A[i,k]] 在 matlab 上工作。

在 matlab 上:当我输入 A[[i,j]] 作为 A,输入 sum[A[i,k]] 作为 (sum(A,2))' 或 sum(A,1) 时,P 在matlab 变成列向量而不是 8 x 8 矩阵。

我错过了什么?

有很多方法可以做到这一点。我将向您展示一种使用原生矢量化 MATLAB 代码的方法,因此不需要 for 循环或 arrayfun 或类似的东西。

A = [1, 1, 1, 0, 0, 1, 0, 1; 0, 0, 1, 0, 0, 1, 0, 1; 1, 1, 0, 1, 0, 1, 1, 0; 0, 1, 1, 0, 1, 0, 1, 0; 1, 1, 1, 1, 0, 1, 1, 1; 1, 1, 1, 0, 0, 1, 1, 0; 1, 0, 1, 0, 1, 0, 1, 0; 0, 0, 0, 0, 1, 0, 0, 1];
n = size(A, 1);
delta = 0.85; 

% First, sum up all columns in each row
rowSums = sum(A,2); 

% Then replicate them because the entries in each row of the vector are valid for every column in that specific row. So replicate them so that the output-matrix matches the size of A so we can work element-wise division later
rowSums2 = repmat(rowSums, 1, n);

% Use vectorized code, ./ will yield element-wise division as A and rowSums2 are of the same size now
P = (1 - delta)/n + delta*A ./ rowSums2

我希望我得到了想要的输出。