我目前正在尝试为高斯马尔可夫随机场创建精度矩阵。假设我在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);
我试过这样的事情,但我不认为这是最有效的方式。我认为应该有更好的方法。
答案 0 :(得分:1)
有点太晚但对其他人可能有用: 您的精度矩阵是对称Toeplitz矩阵的kronecker乘积的线性组合:每个邻居类型对应于2 Toeplitz矩阵的kronecker乘积。 More info about toeplitz Matrix
示例: 你想要一个只有每个像素的水平链接的精度矩阵
写I_n
大小为n
的身份矩阵和H_{n,p}
尺寸为[n n]
的对称Toeplitz矩阵,其中第0个对角线上的所有区域都填充0
H_ {4,2} =
0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0
在Matlab中:
H_nonSym_n_p = toeplitz(零(n,1),[[zeros(1,p-1)1] 0(1,n-p)]);
H_sym_n_p = H_nonSym + H_nonSym&#39; ;
然后,如果你有一个[n m]
图像,如果你想编码每个像素的水平邻居,你可以通过kronecker产品表达它(希望类似LaTeX的代码可以工作):Q = - I_n \ otimes \ H_ {n,2}。
最后,获得你的nnbs:像Q = Q - diag(sum(Q,2))
......
现在,如果你想要其他链接,例如2个水平和2个垂直链接:Q = - I_n \ otimes \ H_ {n,2} - I_n \ otimes \ H_ {n,3} - \ H_ {n, 2} \ otimes I_ {n} - \ H_ {n,3} \ otimes I_ {n}。
再次Q = Q - diag(sum(Q,2))
请注意,对角邻居有点难以生成,但它仍然由toeplitz矩阵的kronecker积(这次可能是非对称的)表示。