Home > Blockchain >  What is the equivalent of coo_matrix in Matlab?
What is the equivalent of coo_matrix in Matlab?

Time:02-04

I'm trying to translatethe following lines of code from Python to MATLAB. V, Id, and J are of size (6400,) which in MATLAB are 1 -by- 6400 row vectors. pts is of size 242.

My Python code

A = coo_matrix((V, (Id, J)), shape=(pts.size, pts.size)).tocsr()
A = A.tobsr(blocksize=(2, 2))

I translated the first line as follows to MATLAB

A = sparse(V,Id,J,242,242);

However, I got the error

Error using sparse
Index into matrix must be an integer.

How can I translate this code to MATLAB?

CodePudding user response:

The MATLAB sparse function has several forms:

S = sparse(A)
S = sparse(m,n)
S = sparse(i,j,v)
S = sparse(i,j,v,m,n)
S = sparse(i,j,v,m,n,nz)

The form you are most likely looking for is the fourth one: S = sparse(i,j,v,m,n), and will want to call it (using your use case) as:

A = sparse(Id, J, V, 242, 242);

I think your error is that MATLAB wants the I and J indices first, followed by the value and you are making the value the first argument.

  •  Tags:  
  • Related