I'm working on some code which looks like the following:
import numpy as np
A=np.array([[1,0,3,5,7],[4,0,6,2,3]])
def SMD(matrix):
if isinstance(matrix,np.ndarray)==False:
raise ValueError('The needed datatype is an array')
else:
m= matrix.shape[0]
n= matrix.shape[1]
a=np.array([])
b=np.array([0])
c=np.array([])
for i in range(m):
for j in range(n):
if matrix[i][j] !=0:
np.append(a,matrix[i][j])
np.append(c,j)
np.append(b,len(a))
return a,b,c
However, the numpy append does not work for me in this case. If I instead use lists instead of arrays, the code runs just fine:
def SMD(matrix):
if isinstance(matrix,np.ndarray)==False:
raise ValueError('The needed datatype is an array')
else:
m= matrix.shape[0]
n= matrix.shape[1]
d=[]
e=[0]
f=[]
for i in range(m):
for j in range(n):
if matrix[i][j] !=0:
d.append(matrix[i][j])
f.append(j)
e.append(len(d))
return d,e,f
the wanted output is:
[1, 3, 5, 7, 4, 6, 2, 3], [0, 4, 8], [0, 2, 3, 4, 0, 2, 3, 4]
Or as arrays (depending on the code used).
Of course, I would like to know why the first code is not working.
From my knowledge, it can be preferable in terms of computation speed to use arrays, but in this case, does it make a difference?
Thanks