我目前正在尝试为高斯马尔可夫随机场创建一个精度矩阵。假设我在 6x6 的空间网格中有随机变量。然后我将有一个 36x36 的精度矩阵。
现在假设我有一个 3x3 的邻居,那么我的精度矩阵将是
Q= nnbs[1] -1 0 0 0 0 -1.......0
-1 nnbs[2] -1 0 0 0 0 ......0
0 -1 nnbs[3] -1 0 0 0 ......0
...................................................
...................................................
等等。谁能建议我如何编码这个精度矩阵。我的意思是如果我将窗口大小/邻域大小更改为 5x5,那么我将有一个新的精度矩阵。我该如何编码?其中 nnbs 是该元素的邻居数
rows=20;
columns=20;
%Random initialization
data=zeros(1000,3);
index=1;
value=-1;
%3x3 neighborhood
%For each element the neighbors are accessible within 1 hop so neighbors=1
neighbors=1;
for i=1:rows
for j=1:columns
for k=1:neighbors
%same row right
if j+k <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((i-1)*columns) + (j+k);
data(index,3) = value;
index=index+1;
end
%same row left
if j-k >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((i-1)*columns) + (j-k);
data(index,3) = value;
index=index+1;
end
end
%row below -> bottom left right
for k=i+1:i+neighbors
if k <= rows
%bottom
data(index,1) = (i-1)*columns+j;
data(index,2) = (k-1)*columns + j;
data(index,3) = value;
index=index+1;
for l=1:neighbors
%right
if j+l <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j+1);
data(index,3) = value;
index=index+1;
end
%left
if j-l >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns)+(j-1);
data(index,3) = value;
index=index+1;
end
end
end
end
%row above top left right
for k=i-1:i-neighbors
if k >= 1
%top
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) +j;
data(index,3) = value;
index=index+1;
for l=1:neighbors
%right
if j+l <= columns
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j+1);
data(index,3) = value;
index=index+1;
end
%left
if j-k >= 1;
data(index,1) = (i-1)*columns+j;
data(index,2) = ((k-1)*columns) + (j-1);
data(index,3) = value;
index=index+1;
end
end
end
end
end
end
%Get the values for the diagonal elements(which is equal to the number of
%neighbors or absolute sum of the nondiagonal elements of the corresponding
%row)
diagonal_values = zeros(rows*columns,3);
for i=1:rows*columns
pointer=find(data(:,1) == i);
diag_value=abs(sum(data(pointer,3)));
diagonal_values(i,1) = i;
diagonal_values(i,2) = i;
diagonal_values(i,3) = diag_value;
end
data(index:index+rows*columns-1,:)=diagonal_values(:,:);
Q = sparse(data(:,1), data(:,2), data(:,3), rows*columns, rows*columns);
我尝试过这样的事情,但我认为这不是最有效的方法。我认为应该有更好的方法。