标记矩阵中的相邻点

时间:2016-06-21 13:30:00

标签: r algorithm

我有一个8x8矩阵:

Compiling C++ myFile.C

[ERROR]: Translator execution failed. Please consult the Troubleshooting section of the User Manual.

Translator returned status 139:

“/usr/include/c++/4.3/atomicity.h”, line 51: warning identifier

            “__sync_fetch_and_add” is undefined

{ return __sync_fetch_and_add(__mem, __val); }

“/usr/include/c++/4.3/atomicity.h”, line 55: warning identifier

            “__sync_fetch_and_add” is undefined

{  __sync_fetch_and_add(__mem, __val); }

“/usr/include/c++/4.3/new”, line 95: warning: first parameter of allocation

                            Function must be of type “size_t”

            Void* operator new(std::size_t) throw (std::bad_alloc);

“/usr/include/c++/4.3/new”, line 96: warning: first parameter of allocation

                            Function must be of type “size_t”

            Void* operator new[](std::size_t) throw (std::bad_alloc);

“/usr/include/c++/4.3/new”, line 99: warning: first parameter of allocation

                            Function must be of type “size_t”

            Void* operator new(std::size_t, const std::nothrow_t&) throw ();

“/usr/include/c++/4.3/new”, line 100: warning: first parameter of allocation

                            Function must be of type “size_t”

            Void* operator new[](std::size_t, const std::nothrow_t&) throw ();

“/usr/include/c++/4.3/new”, line 105: warning: first parameter of allocation

                            Function must be of type “size_t”

            Inline void* operator new(std::size_t, void* __p) throw (){ return __p; }

“/usr/include/c++/4.3/new”, line 105: warning: first parameter of allocation

                            Function must be of type “size_t”

            Inline void* operator new[](std::size_t, void* __p) throw (){ return __p; }

“/opt/ilog51/views51/include/ilog/list.h”, line 77: warning: first parameter of allocation function must be of type “size_t”

IL_MLK_DECL();

“/opt/ilog51/views51/include/ilog/list.h”, line 110: warning:  no appropriate operator delete is visible

{ e(); delete_first; _first; _first = _last 0; _length = 0; }

创建它的代码:

1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1

那些已被提取为坐标:

examplemat <- matrix(c(1, 1, rep(0, 6), 1, rep(0, 9), 1, 1, rep(0, 17), 1, rep(0, 7), 1, rep(0, 5), 1, rep(0, 11), 1), 8, 8, byrow=T)

我需要一种简单的矢量化方法,将这些方法聚类成相邻坐标的组。 (出于此任务的目的,我希望4向上/向下/向左/向右邻接,但您可能还需要包括对角线在内的8向邻接选项。)

在这个例子中,我们最终会得到5个细胞簇:

onecoords <- which(examplemat == 1, arr.ind=T)

      row col
 [1,]   1   1
 [2,]   2   1
 [3,]   1   2
 [4,]   3   3
 [5,]   3   4
 [6,]   7   4
 [7,]   5   6
 [8,]   6   6
 [9,]   8   8

4向邻接检查非常简单: row col clus [1,] 1 1 A [2,] 2 1 A [3,] 1 2 A [4,] 3 3 B [5,] 3 4 B [6,] 7 4 C [7,] 5 6 D [8,] 6 6 D [9,] 8 8 E 但我正在努力研究如何有效地对其进行矢量化。

1 个答案:

答案 0 :(得分:3)

感谢alexis_laz和Carl Witthoft提供的非常有用的评论,让我自己回答。

四方邻接

要获得基于4向邻接的聚类,请在曼哈顿距离上使用单链接的层次聚类:

cbind(onecoords, clus = cutree(hclust(dist(onecoords, "manhattan"), "single"), h = 1))
      row col clus
 [1,]   1   1    1
 [2,]   2   1    1
 [3,]   1   2    1
 [4,]   3   3    2
 [5,]   3   4    2
 [6,]   7   4    3
 [7,]   5   6    4
 [8,]   6   6    4
 [9,]   8   8    5

八向邻接

要获得基于8向邻接(包括对角线)的聚类,请使用最大距离上具有单个链接的层次聚类:

cbind(onecoords, clus = cutree(hclust(dist(onecoords, "maximum"), "single"), h = 1))

曼哈顿与最大距离之间的差异

在示例中,这两者都给出相同的结果,但是如果删除第一行,例如,您将看到曼哈顿距离产生6个聚类,而最大距离产生5个聚类。

cbind(onecoords[2:9,], clus = cutree(hclust(dist(onecoords[2:9,], "manhattan"), "single"), h = 1))
     row col clus
[1,]   2   1    1
[2,]   1   2    2
[3,]   3   3    3
[4,]   3   4    3
[5,]   7   4    4
[6,]   5   6    5
[7,]   6   6    5
[8,]   8   8    6
cbind(onecoords[2:9,], clus = cutree(hclust(dist(onecoords[2:9,], "maximum"), "single"), h = 1))
     row col clus
[1,]   2   1    1
[2,]   1   2    1
[3,]   3   3    2
[4,]   3   4    2
[5,]   7   4    3
[6,]   5   6    4
[7,]   6   6    4
[8,]   8   8    5