1

I am new to Python, and I try to convert code from MATLAB to Python (numpy) .

I have the following code in MATLAB that create sparse matrix:

obj{i,j} = sparse(IR,IV,ones(length(val),1),length(ids{i}),length(vid{j}));

IR & IV are vectors. What is the efficient way to implement that function in Python (numpy) ?
Thanks, MAK

4
  • My answer in stackoverflow.com/questions/45547924/… should work. Get back to me if you have trouble translating the terminology from one to the other. Commented Aug 7, 2017 at 19:19
  • First, Thanks for your help! but, I try to use your example , but I received exception : my translation myNet=bsr_matrix((np.ones(len(u)),u,v),N,N) u,v are vector of size 8000 , and N number of size 200 , I received exception of 'int' object is not iterable (in matlab this code work fine), May I put the input parameter opposite ? Thanks for your help Commented Aug 11, 2017 at 10:33
  • I would have tried csr_matrix((np.ones(len(u)), (u, v)), shape=(N, N)). (I call this the coo style inpu. The number of () is important. coo, csr and csc take this style; bsr does not. Commented Aug 11, 2017 at 15:32
  • @hpaulj 4 Thanks!!. Commented Aug 11, 2017 at 15:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.