Arrays 使用R.MATLAB从从MATLAB导入的数组中提取单个矩阵
我在Matlab中有一个由十个36 x 36矩阵组成的数组。我将名为A的数组保存为.mat。然后,我使用R包R.matlab导入该文件。我试图使用R包pcalg中的函数shd来找到数组A中十个矩阵中每个矩阵的结构汉明距离。对于法线矩阵,执行此操作的过程如下Arrays 使用R.MATLAB从从MATLAB导入的数组中提取单个矩阵,arrays,r,matlab,list,matrix,Arrays,R,Matlab,List,Matrix,我在Matlab中有一个由十个36 x 36矩阵组成的数组。我将名为A的数组保存为.mat。然后,我使用R包R.matlab导入该文件。我试图使用R包pcalg中的函数shd来找到数组A中十个矩阵中每个矩阵的结构汉明距离。对于法线矩阵,执行此操作的过程如下 library(pcalg) library(R.matlab) testfile = readMat("SingleMatrix.mat") testfile = as.matrix(testfile$SingleMatrix) libra
library(pcalg)
library(R.matlab)
testfile = readMat("SingleMatrix.mat")
testfile = as.matrix(testfile$SingleMatrix)
library(igraph)
Test=as.graphnel(graph.adjacency(testfile, weighted = T))
shd(Correct,Test)
这给出了结构汉明距离,它只是一个将正确矩阵与测试矩阵进行比较的数字
当数组从Matlab导入R时,如何对列表列表执行此操作?我想要十个矩阵中每一个的结构汉明距离。有没有一种方法可以自动将它们取出并转换为graphnel类型,然后进行比较
导入数据的结构如下所示:
str(testfile)
List of 1
$ K210000ALARM:List of 10
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 1 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
..$ :List of 1
.. ..$ : num [1:37, 1:37] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "header")=List of 3
..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Tue Jan 31 06:58:46 2017 "
..$ version : chr "5"
..$ endian : chr "little"
此代码重新创建数据的结构
testfile <- list(lapply(1:10, function(i) list(matrix(i:(8+i), 3))))
第二个是
testfile[[1]][[2]][[1]]
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9
[3,] 4 7 10
矩阵列表
要将结构展平为矩阵列表(这可能是它们的最佳结构),可以使用
myMatList <- lapply(unlist(testfile, recursive=FALSE), function(i) unlist(i))
# get array of correct dimensions with 0s
myArray <- array(0, c(dim(testfile[[1]][[2]][[1]]), 10))
# fill it in
for(i in 1:10) myArray[,,i] <- testfile[[1]][[i]][[1]]
返回
myArray[,,1:2]
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9
[3,] 4 7 10
获取距离
对于矩阵列表,以下内容可能适用
myHamsList <- lapply(myMatList, function(i) shd(Correct,
as.graphnel(graph.adjacency(testfile, weighted = T))))
myHamsList
myHamsList <- lapply(myMatList, function(i) shd(Correct,
as.graphnel(graph.adjacency(testfile, weighted = T))))