Ask Your Question
0

From csv to matrix

asked 2020-05-27 23:57:49 +0200

Cyrille gravatar image

updated 2020-05-28 08:28:23 +0200

I have this file copied of an other question

pcb138,pcb180,pcb52,pcb118,pcb
1.46,0.738,0.532,0.72,19.9959
0.64,0.664,0.03,0.236,6.0996 
3.29,1.15,0.134,1.54,24.9655
3.94,1.33,0.466,1.94,37.4436
3.18,2.14,0.243,1.47,30.183 
2.43,1.3,0.137,1.31,20.8036
3.94,3.49,0.208,0.876,41.3818
3.38,1.04,0.477,2.46,29.478 
2.21,0.966,0.457,1.14,24.2387
2.49,1.59,0.298,1.18,26.3198
0.86,0.395,0.02,0.406,8.591
3.38,1.85,0.539,1.5,36.4229
7.39,4.42,0.707,3.55,66.4108
2.49,1.59,0.298,1.18,26.3198
0.86,0.395,0.02,0.406,8.591
3.38,1.85,0.539,1.5,36.4229
7.39,4.42,0.707,3.55,66.4108

It is saved in a file name pcb.dat. With the following code

import csv                           
file=r'C:\Users\Cyrille\pcb.dat'
reader=csv.reader(open(file))
L=[]
for row in reader:                   
    L.append(row)

show(L)

I can import it in SM. But it's a list composed of strings. And I want a matrix beginning at the second line with Real or Rational numbers. How can I obtain this little and simple (but absolutely necessary) operation ? Therer is a lot of questions on that subject but I cannot see in the answer the one I am searching.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-05-28 00:44:02 +0200

tmonteil gravatar image

Since you want to discard the first entry of L, you can make the slice :

sage: L[1:]
[['1.46', '0.738', '0.532', '0.72', '19.9959'],
 ['0.64', '0.664', '0.03', '0.236', '6.0996 '],
 ['3.29', '1.15', '0.134', '1.54', '24.9655'],
 ['3.94', '1.33', '0.466', '1.94', '37.4436'],
 ['3.18', '2.14', '0.243', '1.47', '30.183 '],
 ['2.43', '1.3', '0.137', '1.31', '20.8036'],
 ['3.94', '3.49', '0.208', '0.876', '41.3818'],
 ['3.38', '1.04', '0.477', '2.46', '29.478 '],
 ['2.21', '0.966', '0.457', '1.14', '24.2387'],
 ['2.49', '1.59', '0.298', '1.18', '26.3198'],
 ['0.86', '0.395', '0.02', '0.406', '8.591'],
 ['3.38', '1.85', '0.539', '1.5', '36.4229'],
 ['7.39', '4.42', '0.707', '3.55', '66.4108'],
 ['2.49', '1.59', '0.298', '1.18', '26.3198'],
 ['0.86', '0.395', '0.02', '0.406', '8.591'],
 ['3.38', '1.85', '0.539', '1.5', '36.4229'],
 ['7.39', '4.42', '0.707', '3.55', '66.4108']]

Now, you can turn this list into a matrix over the RealDoubleField (RDF) as follows:

sage: M = matrix(RDF,L[1:])
sage: M
[   1.46   0.738   0.532    0.72 19.9959]
[   0.64   0.664    0.03   0.236  6.0996]
[   3.29    1.15   0.134    1.54 24.9655]
[   3.94    1.33   0.466    1.94 37.4436]
[   3.18    2.14   0.243    1.47  30.183]
[   2.43     1.3   0.137    1.31 20.8036]
[   3.94    3.49   0.208   0.876 41.3818]
[   3.38    1.04   0.477    2.46  29.478]
[   2.21   0.966   0.457    1.14 24.2387]
[   2.49    1.59   0.298    1.18 26.3198]
[   0.86   0.395    0.02   0.406   8.591]
[   3.38    1.85   0.539     1.5 36.4229]
[   7.39    4.42   0.707    3.55 66.4108]
[   2.49    1.59   0.298    1.18 26.3198]
[   0.86   0.395    0.02   0.406   8.591]
[   3.38    1.85   0.539     1.5 36.4229]
[   7.39    4.42   0.707    3.55 66.4108]

sage: M.parent()
Full MatrixSpace of 17 by 5 dense matrices over Real Double Field

Sage automatically transforms the strings into elements of RDF.

edit flag offensive delete link more

Comments

Super Merci

Cyrille gravatar imageCyrille ( 2020-05-28 07:43:58 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-05-27 23:57:49 +0200

Seen: 505 times

Last updated: May 28 '20