0

I've three csv files:
v.csv for vertex data, like this:
1
2
3
...

e.csv for edges:
1,2
2,4
2,24
3,1
...

and vcoor.csv for vertex coordinates:
23.4, 35.2
23.4, 40.3
...

I know how to construct a simple graph in Mathematica, but after importing edges by Import, I can't use them in Graph

v = Import["C:\\Users\\MST\\Desktop\\v.csv", "List"];
e = Import["C:\\Users\\MST\\Desktop\\e.csv", "Table"];
vcoor = Import["C:\\Users\\MST\\Desktop\\vcoor.csv", "Table"];
g = Graph[v, e, VertexCoordinates -> vcoor];
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 1
    Please show exactly what you tried so far. – Szabolcs Mar 08 '16 at 22:40
  • 1
    We do not have your data, so unless you provide fully functional code and data to reproduce the problem we will probably not be able to help you. – Yves Klett Mar 09 '16 at 08:55
  • 4
    CSV files must be imported as "CSV", not as "Table". Have you looked at what e looks like after importing? Does it have a structure suitable for Graph? Did you look up Graph in the documentation to see what form of input it accepts? It must be an edge list, i.e. if you have e={{1,2},{3,4}} you must use UndirectedEdge @@@ e. – Szabolcs Mar 09 '16 at 09:08
  • Files is too big to upload and can't be truncated, and I know there is no problem in my data. The main problem is Graph function does not accept table (two column matrix) for edges/VertextCoordinates, and I don't know how to deal with it. – Mostafa Abasinejad Mar 09 '16 at 09:12
  • Very thanks to @Szabolcs! I'm MATLAB coder and I'm new to Mathematica. I read documentation, but I could not find a way to build the edge list form table. – Mostafa Abasinejad Mar 09 '16 at 09:15

2 Answers2

2

Remove the table option in your Import. Then, write:

g = Graph[v, UndirectedEdge@@@ e, VertexCoordinates -> vcoor]
Trad Dog
  • 450
  • 4
  • 9
2

Input

(*arbitrary data*)
vertices = Range[10];
edges = #1 <-> #2 & @@@ RandomInteger[{1, 10}, {10, 2}];
coordinates = RandomReal[{1, 50}, {10, 2}];

Process

(*process*)
Graph[vertices, edges, VertexCoordinates -> coordinates]

Output

output image

Reference

# & @ @@ etc.
Graph
Range
RandomReal
RandomInteger

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32