How can I change the input method by using xlsread and convert the code?
Ältere Kommentare anzeigen
I have this DFS algorithm code down below, and I wanna convert it properlly so it would read an excel file "input.xls" with the command "m=xlsread('input', 1, xlrange )" which contains the following table and then use it as input to run the DFS. Any ideas?
the table is something like this:
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
and the beginning of the code I want to convert:
m=dlmread('input.txt',',');
r=zeros(m(1,1),m(1,1));
i=2;
while (i+1<=length(m))
r(m(1,i),m(1,i+1))=1; % creates a table with 0 and 1 %
i=i+2;
end
R=r;
dfnr=zeros(1,m(1,1));
T=zeros(1,m(1,1));
stack=zeros(1,m(1,1));
to3odentrou=R;
k=1;
j=1;
while (r(k,j)==0) % check the table to find where there are no zeros %
j=j+1;
if (j==m(1,1)+1)
k=k+1;
j=1;
end
end
r(k,j)=0; % last checked element
%
a=1;
stack(1,a)=k;
stack(1,a+1)=j;
if (stack(1,a)==1)
T(1,k)=0;
T(1,j)=1;
else
T(1,k)=(stack(1,a))-1;
T(1,j)=(T(1,k))-1;
end
5 Kommentare
Walter Roberson
am 3 Nov. 2018
Why not just change
m=dlmread('input.txt',',');
to
xlrange = something appropriate
m = xlsread('input', 1, xlrange );
John Smith
am 3 Nov. 2018
dpb
am 3 Nov. 2018
"the variables need some change because the code stacks"
What in the world does the above mean, precisely?
SHOW us what the problem is, specifically...
Walter Roberson
am 3 Nov. 2018
It appears that the input expected for this routine is a matrix of data that contains size and link information, and it appears that you would like to instead directly import the results of building the r matrix. There are, however, a few important variables that are built along the way, and someone would need to analyze to figure out if they can be built just from the completed r matrix.
John Smith
am 5 Nov. 2018
Akzeptierte Antwort
Weitere Antworten (1)
Alternatively, rather than trying to fix code that has no comments explaining what is going on and poor variable names, you could just get rid of it all, and use the built-in dfsearch. Most likely, something like:
g = digraph(xlsread('input', 1, xlrange));
T = dfsearch(g, 1, 'allevents')
with the appropriate start node and other options for dfsearch.
Kategorien
Mehr zu Data Import and Export finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!