Mathematica SparseArray Access Non-Default Values

  • Mathematica
  • Thread starter wima
  • Start date
  • Tags
    Mathematica
In summary, the conversation was about accessing the number of non-zero values in each row of a large sparse matrix. Suggestions were made to use Normal[test[[1]]] or Length[ArrayRules[test[[1]]]] - 1 to retrieve this information. The issue of converting the entire matrix into a normal array was also addressed.
  • #1
wima
3
0
Hi,

I am currently dealing with really huge (~ 80,000 x 80,000) sparse matrices. I this context I need to read the number of non-default values in each row.
When Accessing the i-th row of a SparseArray, this information is given.
E.g.
test = SparseArray[{{1, 1} -> 3, {1, 5} -> 2, {5, 3} -> 1}];
test[[1]]
returns SparseArray[<2>, {5}]
However, I fail to access the number of entries (here 2) to assign it to a variable.
I did not find any information in the documentation so far.
Does anyone have an idea?

Thx
 
Physics news on Phys.org
  • #3
I could be wrong but is it :
test[[1]][[5]] in this case? its the only index that returns "2"
 
  • #4
@DaleSpam:
The problem is, that my matrices are too large to convert them into normal arrays. However, about only 1% of the matrix entries have non-zero values. For my computations, I need to construct a vector that stores the number of non-zero values that are in each row of the matrix. For the example I posted, that vector would be {2,0,0,0,1} because there are two entries in the first row and one in the fifth row. Iterating over all 80,000 x 80,000 = 6,400,000,000 values of the sparse matrix will not work...

@Hepth:
test[[1]][[5]] returns the value of the matrix in row 1 and column 5. That entry is 2. What I need to access is the number of nonzero values in every row.

It's really bothering, because it's obvious that the number of entries is easily accessible as you can see here:
test[[1]]
test[[2]]
test[[5]]
returns
SparseArray[<2>, {5}]
SparseArray[<0>, {5}]
SparseArray[<1>, {5}]
namely 2, 0 and 1. However, it's not sufficient for me to read the values manually, but I need to assign them to a variable for storing them in the vector of size 80,000. So the probelm is to access the values in <>.

Anyway, thanks for your replies so far!
 
  • #5
Yes, I understand that it is too big to convert the whole matrix into a normal array. That is why I suggest Normal[test[[1]]] rather than Normal[test][[1]]. For your matrix that will be a vector with 80k entries, so not too big.

The values in the <> are not real values stored anywhere, they are just for display. If you really want those the only way to get them would be to use ToString[test[[1]]] and then use some string manipulation to pull out the number between the <>.
 
  • #6
Length[ArrayRules[test[[1]]]] - 1

will give you the number of nonzero elements. But Array Rules itself might not be fast for something that large, it gives the list of all non zero components.
 
  • #7
Thank you for your replies! All of the suggested methods work. I am currently about to figure out, which of them are most efficient.
 

What is a SparseArray in Mathematica?

A SparseArray is a data structure in Mathematica used for storing large arrays with mostly zero values efficiently. It only stores the non-zero values and their corresponding positions, making it useful for working with large and sparse datasets.

How do I access non-default values in a SparseArray?

You can access non-default values in a SparseArray by using the Part function, also denoted by double brackets ([[ ]]), with the index of the desired element. For example, if sparseArray is your SparseArray object, you can access the non-zero value at index 2 by using sparseArray[[2]].

Can I modify the non-default values in a SparseArray?

Yes, you can modify the non-default values in a SparseArray by assigning new values to the corresponding indices using the Part function. However, keep in mind that modifying the values may change the sparsity pattern of the SparseArray and may affect its performance.

How can I convert a regular array to a SparseArray in Mathematica?

You can convert a regular array to a SparseArray in Mathematica by using the SparseArray function and passing in the array as an argument. It will automatically convert it to a SparseArray, keeping only the non-zero values and their corresponding positions.

Is there a way to create a SparseArray with a specific sparsity pattern?

Yes, you can create a SparseArray with a specific sparsity pattern by using the SparseArray function and specifying the dimensions and the positions of the non-zero values as arguments. This can be useful for creating a SparseArray with a known structure and filling in the values later.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
257
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
762
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
4
Views
863
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top