从单元阵列创建邻接矩阵

问题描述 投票:2回答:1

我将从单元阵列创建一个邻接矩阵,但面临两个主要问题:

  1. 我不知道如何访问单元格数组元素;使用ad hoc方法。
  2. (也是最重要的)代码产生错误,部分结果也是奇怪的!

单元格数组如下图所示:screen shot of MATLAB's variable editor showing the contents of a cell array

代码如下:

for i=1:N
    L=size(Al{i});
    Len=L(1,2);
    for j=1:Len
        elm=Al{i};
        D=elm(i,j);
        Adjm(i,D)=1;
    end
end

代码产生此错误:

screen shot of the MATLAB command window showing an error message

输出如下:yet another screen shot showing defined variables

P.S。:该代码是构建邻接矩阵以表示图像内的超像素邻接的程序的一部分。可能有一个特定的解决方案!

matlab image-processing matrix image-segmentation
1个回答
4
投票

有很多方法可以使你的代码变得更好,但你看到的具体错误是因为你想要D=elm(1,j);而不是D=elm(i,j);。注意1而不是i

一种更有效的方法是做,

for i=1:numel(Al)
    Adjm(i,Al{i})=1;
end

与您的代码一样,这假设Al中没有空元素。

© www.soinside.com 2019 - 2024. All rights reserved.