You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's say I have a large, sparse matrix of zeros. As my program runs, it changes values througout the matrix, though I may not know which values are changed until after its done. Is there a quick way to get the positions of all values that meet a criteria?
For example, I make a sparse matrix, and assign random values to random places:
Where getRandomInt returns a random integer in the range given. Here is a sandbox to play with that runs the above code. Is there a way to quickly get the positions and values that were set? Or even get the positions of all values that equal a certain number (say, 5)?
When I examine the matrix object in the console, I see the ._values property, which has the values, and I also see a ._index, which has the same length as ._values, and ._ptr:
I'm not sure how these values are used exactly to keep track of values in the matrix, and I can't find any documentation on it. I feel like this must be trivial, but I'm missing something. How can I say, "get me all the x, y positions of all values that satisfy a requirement (i.e. !== 0, === 5, > 2, etc)"?
The text was updated successfully, but these errors were encountered:
Hey Seth!
The easiest way to access all the non-zero entries of a sparse matrix is by using its iterator:
constM=math.zeros([50,30],'sparse');for(leti=0;i<100;i++){constposition=[rand(0,50),rand(0,30)];constvalue=rand(1,9);M.set(position,value);}for(const{ index, value }ofM){if(value>2){doSomething(index)// here's your index}}
Alternatively, you can use M.forEach wich works in a similar way (with slight differences – eg. you can't terminate it halfway through).
If you wanted to use the { _ptr, _index, _value } properties directly, you can check out the implementation of the iterator. There you can see what each property does: _value holds all non-zero values, _index tells you the row of each value and _ptr has “indices of indices” – it points you to the first index of each row.
I know that the documentation for sparse matrices is quite underwhelming right now, it's on our compass :)
Let's say I have a large, sparse matrix of zeros. As my program runs, it changes values througout the matrix, though I may not know which values are changed until after its done. Is there a quick way to get the positions of all values that meet a criteria?
For example, I make a sparse matrix, and assign random values to random places:
Where
getRandomInt
returns a random integer in the range given. Here is a sandbox to play with that runs the above code. Is there a way to quickly get the positions and values that were set? Or even get the positions of all values that equal a certain number (say, 5)?When I examine the matrix object in the console, I see the
._values
property, which has the values, and I also see a._index
, which has the same length as._values
, and._ptr
:I'm not sure how these values are used exactly to keep track of values in the matrix, and I can't find any documentation on it. I feel like this must be trivial, but I'm missing something. How can I say, "get me all the x, y positions of all values that satisfy a requirement (i.e.
!== 0
,=== 5
,> 2
, etc)"?The text was updated successfully, but these errors were encountered: