I came across an interesting exercise that I would like to try to solve with Mathematica to improve my skills. In data is provided to build a distance matrix. The data looks like this, starting from node0
to node99
:
node0, node1 0.04, node8 11.11, node14 72.21
node1, node46 1247.25, node6 20.59, node13 64.94
node2, node66 54.18, node31 166.80, node45 1561.45
node3, node20 133.65, node6 2.06, node11 42.43
I was just wondering how would you start doing this. I imported the file using Import["file.txt", "Data"]
. I've been trying to make strings of each line and to drop the 1st element of the string, but I can't seem to get this. I started by doing:
number_nodes=100
max_node=99
data= Import["file.txt", "Data"]
Currently stuck at this stage Any help would be greatly appreciated :)
data = ImportString["paste_given_data_here", "Table", "FieldSeparators" -> {",", " ", "node"}]
. Since in Mathematica lists start at index 1, not 0, you can then adjust the node numbers by adding 1 to any integer in the results:data /. a_Integer :> a + 1
. I have to say, though, that the format in that exercise is rather awful to work with. $\endgroup$n = 100; max_number = 99; Q = ConstantArray[Infinity, {n, n}]; Q // MatrixForm; data = ImportString["data", "Table", "FieldSeparators" -> {",", " ", "node"}]; node = Drop[data, 0, 1]
. Is this good? Im planning to make this an interfer this run a if loop and fill the matrix i made. $\endgroup$