将稀疏矩阵扩展/转换为较大的稀疏矩阵 [英] Extending / converting a sparse matrix into a larger sparse matrix
问题描述
我知道这个问题的标题很容易混淆,即使没有错.抱歉,让我解释一下我尝试做的事情:
I am aware that the title of this question is confusing, if not wrong. Sorry for this, let me explain what I try to do:
# I have a population of individuals:
population <- c("Adam", "Bob", "Chris", "Doug", "Emily", "Frank", "George","Harry", "Isaac", "Jim", "Kyle", "Louis")
population_size <- length(population) # this is 12
# I then draw a sample from this population
mysample_size <- 5
mysample <- sample(population,mysample_size, replace=FALSE)
# I then simulate a network among the people in the sample
frn <- matrix(rbinom(mysample_size*mysample_size, 1, 0.4),nrow=n)
x[x<=0] <- 0
x[x>0] <- 1
rownames(frn) <- mysample
colnames(frn) <- mysample
*我现在想将值从frn转移到一个矩阵中,该矩阵包括原始种群的所有成员,即12 x 12矩阵.该矩阵中的值只能来自frn 5 * 5矩阵.
*I would now like to transfer the values from frn into a matrix that includes all members from the original population, i.e. a 12 by 12 matrix. The values in that matrix would only come from the frn 5*5 matrix.
我不知道如何从顶部的矩阵生成底部的矩阵.
I do not know how to generate the matrix at the bottom from the matrix at the top.
我想到了不同的方式(例如,使用iGraph并通过边列表进行推进)或运行循环,但实际上并没有一种单独的方法可以运行.作为背景可能很重要:我的实际矩阵远大于此,并且我需要多次运行此操作,因此有效的解决方案将是一个很好的解决方案.非常感谢您的帮助.
I have thought of different ways (e.g. using iGraph and advancing via edgelists) or running loops, but did not really get a single alternative to run. Maybe important to know as background: My actual matrices are much larger than this and I need to run this operation many times, thus an efficient solution would be great. Thanks a lot for your help.
推荐答案
最简单的解决方案:ind = match(mysample,population)
为您提供与样本对应的行和列的索引号,因此可以通过popn[ind,ind] = frn
.完成.
Neatest solution: ind = match(mysample,population)
gives you the index numbers of the rows and columns corresponding to the sample, so update the population network matrix popn
by doing popn[ind,ind] = frn
. Done.
这篇关于将稀疏矩阵扩展/转换为较大的稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!