What means : Error using sparse Index exceeds array bounds.

조회 수: 21 (최근 30일)
JGUIS
JGUIS 2018년 8월 14일
편집: dpb 2018년 8월 14일
Hello, I'm trying to create an adjacency matrix from various files : '_graph_labels.txt', '_graph_indicator.txt', '_node_labels.txt','_node_attributes.txt', '_A.txt' .... when creating the adjacency matrix A, an error occurs : Error using sparse Index exceeds array bounds.
anyone could explain to me what means this error and where it can come from ?
if true
datasets = strvcat('CRIME');
for ith_data = 1: size(datasets, 1)
dataset = strcat(datasets(ith_data, :));
path2data = [dataset '/']; % path to folder containing data
% graph labels
graph_labels = dlmread([path2data dataset '_graph_labels.txt']);
num_graphs = size(graph_labels, 1);
graph_ind = dlmread([path2data dataset '_graph_indicator.txt']);
num_nodes = size(graph_ind, 1);
% node labels
try
labels = dlmread([path2data dataset '_node_labels.txt']);
if size(labels,1) ~= num_nodes
fprintf('ERROR: Wrong number of nodes in %s!\n', [dataset '_node_labels.txt']);
end
catch
disp('No node labels for this dataset.')
end
% node attributes
try
attributes = dlmread([path2data dataset '_node_attributes.txt']);
if size(attributes,1) ~= num_nodes
fprintf('ERROR: Wrong number of nodes in %s!\n', [dataset '_node_attributes.txt']);
end
catch
disp('No node attributes for this dataset.')
end
% edges, adjacency matrix
edges = dlmread([path2data dataset '_A.txt']);
num_edges = size(edges,1);
% edge attributes (e.g. edge weights etc.)
try
edge_attr = dlmread([path2data dataset '_edge_attributes.txt']);
if size(edge_attr, 1) ~= num_edges
fprintf('ERROR: Wrong number of edges in %s!\n', [dataset '_edge_attributes.txt']);
end
if size(edge_attr,2) > 1
fprintf('CAUTION: there are more than one edge attributes in %s!\n', [dataset '_edge_attributes.txt']);
fprintf('CAUTION: only the first one is used in adjacency matrix.\n');
end
catch
edge_attr = ones(num_edges, 1);
disp('No edge attributes for this dataset.')
end
A = spones(sparse(edges(:,1), edges(:,2), edge_attr(:,1), num_nodes, num_nodes));
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 8월 14일
The line of code says that a sparse array is to be used and that it is to be rectangular num_nodes by num_nodes. But some value in edges array is larger than num_nodes so sparse() is not able create the array.
JGUIS
JGUIS 2018년 8월 14일
편집: dpb 2018년 8월 14일
Thank you for your answer, I understand, but the size of the sparse matrix is num_nodes * num_nodes (246*276). Each element in the matrix corresponds to an edge. The size 276*276 implies that there is a maximum of 76176 elements/edges and my file edge contains 2 columns each with 4569 edges/elements. The form of my sparse matrix is :
A = sparse(i,j,v,m,n) with :
-i the first colonm of the file edges
-j the second columns of the file edges
-v the attribute containing 4569 rows and 1 column.
So the size of (i,j,v) is 4569*2, and it contains 9138 elements so it should fit with the imposed size 276*276 of 76176 elements

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2018년 8월 14일
Show us the output of this command:
nnz(edges > num_modes)
The output will likely be nonzero. If that's the case it indicates you're trying to do something like the example below.
% Try to make a 4-by-4 matrix S whose (5, 1) element is 2
S = sparse(5, 1, 2, 4, 4);
You can't tell MATLAB the sparse matrix is going to have 4 rows (the fourth input in that example) then in that same sparse call try to assign a value to an element in the 5th row (the first input.)
Each and every element in the first input to sparse MUST BE less than or equal to the fourth input, and similarly for the second and fifth inputs. Violating this condition results in the error you received.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by