Home > front end >  Conversion of matlab sparse matrix of dimension m by n to python
Conversion of matlab sparse matrix of dimension m by n to python

Time:07-26

I am trying to convert the following matlab code to python :

W2 = sparse(1:m*n-1,2:m*n,-w_alpha(1),m*n,3*m*n);

the output should be in this format in matlab :

   (1,2)      -10
   (2,3)      -10
   (3,4)      -10
   (4,5)      -10
    ..............
   ...............

This is my code in python :

 i=np.int64(np.dot(m,n)-1) #------>m*n-1----->i=102
 test_j=np.int64(np.dot(m,n))#------->m*n 
 j=np.int64(np.dot(3,test_j))  #------>3*m*n----> j=309    
 a=((np.arange(0,i)).reshape(1,-1))
 b=(np.arange(1,test_j)).reshape(1,-1)
 W2=csr_matrix((-w_alpha,(a,b)),shape=(test_j,j)) 

where

 m=103
 n=1
 weights = [10,10,10]
 w_alpha = np.float64(weights[0]);

However, the syntax is not correct . I used following syntax from the scipy library : csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)]) where data, row_ind and col_ind satisfy the relationship a[row_ind[k], col_ind[k]] = data[k].

Besides, I got the following error message : TypeError: len() of unsized object

Any sugesstions to get similar output as matlab .

CodePudding user response:

OK, I think this is what you are after. The basic issue is that, where you were passing -w_alpha, you instead need to pass an array of N copies of -w_alpha.

import numpy as np
from scipy.sparse import csr_matrix

m=103
n=1
weights = [10,10,10]
w_alpha = float(weights[0])

i = m*n-1
test_j = m*n
j = 3*test_j 

a = np.arange(0,i).flatten()
b = np.arange(1,test_j).flatten()
w_alpha = np.array([w_alpha] * len(a))
W2 = csr_matrix((-w_alpha,(a,b)),shape=(test_j,j)) 
print(W2)

Output:

  (0, 1)    -10.0
  (1, 2)    -10.0
  (2, 3)    -10.0
  (3, 4)    -10.0
  (4, 5)    -10.0
  (5, 6)    -10.0
  (6, 7)    -10.0
  (7, 8)    -10.0
  (8, 9)    -10.0
  (9, 10)   -10.0
  (10, 11)  -10.0
  (11, 12)  -10.0
  (12, 13)  -10.0
  (13, 14)  -10.0
  (14, 15)  -10.0
  (15, 16)  -10.0
  (16, 17)  -10.0
  (17, 18)  -10.0
  (18, 19)  -10.0
  (19, 20)  -10.0
  (20, 21)  -10.0
  (21, 22)  -10.0
  (22, 23)  -10.0
  (23, 24)  -10.0
  (24, 25)  -10.0
  : :
  (77, 78)  -10.0
  (78, 79)  -10.0
  (79, 80)  -10.0
  (80, 81)  -10.0
  (81, 82)  -10.0
  (82, 83)  -10.0
  (83, 84)  -10.0
  (84, 85)  -10.0
  (85, 86)  -10.0
  (86, 87)  -10.0
  (87, 88)  -10.0
  (88, 89)  -10.0
  (89, 90)  -10.0
  (90, 91)  -10.0
  (91, 92)  -10.0
  (92, 93)  -10.0
  (93, 94)  -10.0
  (94, 95)  -10.0
  (95, 96)  -10.0
  (96, 97)  -10.0
  (97, 98)  -10.0
  (98, 99)  -10.0
  (99, 100) -10.0
  (100, 101)    -10.0
  (101, 102)    -10.0

CodePudding user response:

In Octave, with w_alpha=10, the display is actually:

W2 =

Compressed Column Sparse (rows = 25, cols = 75, nnz = 24 [1.3%])

  (1, 2) -> -10
  (2, 3) -> -10
  (3, 4) -> -10
  (4, 5) -> -10
  (5, 6) -> -10
  (6, 7) -> -10
  (7, 8) -> -10
  (8, 9) -> -10
  (9, 10) -> -10
   ...

So youmight want to use sparse.csc_matrix instead. But also the arguments must be 1d arrays with matching sizes. Your a and b are (1,102). In MATLAB everthing is 2d, in numpy things can be 1d, and in some cases must be.

w_alpha = a*0 w_alpha
In [170]: a,b = a.ravel(), b.ravel()
In [171]: w_alpha= w_alpha.ravel()
In [172]:  w_alpha = a*0 w_alpha
     ...:  W2=csr_matrix((-w_alpha,(a,b)),shape=(test_j,j))
In [173]: W2
Out[173]: 
<103x309 sparse matrix of type '<class 'numpy.float64'>'
    with 102 stored elements in Compressed Sparse Row format>
In [174]: print(W2)
  (0, 1)    -10.0
  (1, 2)    -10.0
  (2, 3)    -10.0

If you save the MATLAB matrix to a file (old style), scipy.io.loadmat can loaded it as csc.

  • Related