问题
我在算法中使用了一个邻接矩阵,只要我测试了一个小矩阵(3000)点,它就工作得很好。但是我的实际问题包括167620点,我想为这个问题建立一个邻接矩阵。但很明显,由于长度的关系,我遇到了以下问题:
Requested 167620x167620 (209.3GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a
long time and cause MATLAB to become unresponsive.我使用以下代码:
adjMat = zeros(size(NB_list_all,1));
for ind = 1:size(NB_list_all,1)
adjMat(ind, nonzeros(NB_list_all(ind,2:end))) = 1;
end
adjMatS=sparse(adjMat);
G=digraph(adjMatS);
E=table2array(G.Edges); 如你所见,我基本上需要边缘列表。
问题
因此,问题是:是否有一种直接计算稀疏邻接矩阵的方法,或者是否有从邻接列表中获取边缘列表的不同方法?我的NB_list_all包含第1列中的点和2-5列中的邻接点?
编辑
我的NB_list_all是以下形式的附件列表:
1 2 0
2 3 1
3 4 2
4 5 3
5 6 4
6 7 5
7 8 6
8 9 7
9 0 8第一列是点,第2:5列是它相邻的点的独立点。0如果没有邻居。我想要为其创建边缘列表的矩阵是167620x5。在使用matlab的E=table2array(G.Edges);图形函数之前,我创建了边缘列表。现在我基本上有两个问题:
发布于 2016-06-22 13:07:45
如果您知道所有的索引和值,以及矩阵的结束大小,则可以用
adjMat = sparse(indexi,indexj,value,size1,size2);一次一次。实际上,这是创建稀疏矩阵的预先方法。
示例:
您需要以下矩阵:
0 1 1
1 0 0
0 0 1你可以把它建成:
sparse([1 1 2 3], [2 3 1 3],[1 1 1 1],3,3)你举的例子是:
NB_list_all=[
1 2 0
2 3 1
3 4 2
4 5 3
5 6 4
6 7 5
7 8 6
8 9 7
9 0 8];
% if the first index contains all numbers we can safely do this
% this is almost indexJ, but we have some zeroes that we dont like
indexJ=NB_list_all(:,2:end);
% create indexI
indexI=repmat(1:size(indexJ,1),size(indexJ,2),1).';
% lets unroll the matrices
indexI=indexI(:);
indexJ=indexJ(:);
% lets remove the indexI and NB_list_all that have a zero somewhere, because those are not real
notzero=find(indexJ);
indexI=indexI(notzero);
indexJ=indexJ(notzero);
adjMat=sparse(indexI,indexJ,1,size(NB_list_all,1),size(NB_list_all,1));https://stackoverflow.com/questions/37968909
复制相似问题