Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick way to get positions / values of sparse matrix? #2274

Closed
slutske22 opened this issue Jul 5, 2021 · 1 comment
Closed

Quick way to get positions / values of sparse matrix? #2274

slutske22 opened this issue Jul 5, 2021 · 1 comment
Labels

Comments

@slutske22
Copy link

slutske22 commented Jul 5, 2021

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:

const zeros = math.zeros([50, 30], "sparse");

for (let i = 0; i < 100; i++) {
  const position = [getRandomInt(0, 50), getRandomInt(0, 30)];
  const value = getRandomInt(1, 9);
  zeros.set(position, value);
}

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:

Screen Shot 2021-07-05 at 12 25 42 PM

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)"?

@cshaa
Copy link
Collaborator

cshaa commented Jul 5, 2021

Hey Seth!
The easiest way to access all the non-zero entries of a sparse matrix is by using its iterator:

const M = math.zeros([50, 30], 'sparse');

for (let i = 0; i < 100; i++) {
  const position = [rand(0, 50), rand(0, 30)];
  const value = rand(1, 9);
  M.set(position, value);
}

for (const { index, value } of M) {
  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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants