问题
I am trying to build a network graph with word adjacency data. But I am getting the error "Target must be a dense double array of node indices". Following is my code:
fileName = 'adjnoun.gml';
inputfile = fopen(fileName);
A=[];
l=0;
k=1;
while 1
% Get a line from the input file
tline = fgetl(inputfile);
% Quit if end of file
if ~ischar(tline)
break
end
nums = regexp(tline,'\d+','match'); %get number from string
if length(nums)
if l==1
l=0;
A(k,2)=str2num(nums{1});
k=k+1;
continue;
end
A(k,1)=str2num(nums{1});
l=1;
else
l=0;
continue;
end
end
A= sort(A);
g = graph(A(:,1),A(:,2));
A is 425X2 double matrix. When I am trying to create the graph g = graph(A(:,1),A(:,2))
, it is throwing the error.
回答1:
Matlab's graph(s, t)
function will display that error if you have 0's in your source or target arrays. In other words, if A(:, 2)
contains any zeros, Matlab will fail with the displayed error. You could:
i. Add "1" to all of your values with: A=A+1
ii. Modify your original graph to produce a .gml output without zeros.
来源:https://stackoverflow.com/questions/35075759/target-must-be-a-dense-double-array-of-node-indices-how-to-solve