scijs/ndarray

ndarray.set the i, j vector

Opened this issue · 4 comments

Using this library, I have found frequently that I need to set an entire vector at a time when manipulating the ndarray. For example:

const n = ndarray([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]);
const newVector = n.pick(0, 0);
for (let i = 0; i < n.shape[3]; i++) {
    n.set(1, 1, j, newVector.get(j));
}

Is there an intended way to handle this redundancy? Ideally:

const n = ndarray([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]);
const newVector = n.pick(0, 0);
n.set(1, 1, newVector);

Context:
Writing an image processing library. I wrote a median filter for the image. I need iterate over all the pixels and set them to the median of their neighbors.

I think there is no way to do it since the underlying data might not be contiguous in your newVector. Except some cases only you know about, there are no more efficient ways than the one you showed.

I have the same requirement. I need to copy a subset of an ndarray into a different bigger ndarray as efficiently as possible. I think this can be implemented faster than the for loop above by using the compilation techniques that ndarray uses.

You can use ndarray ops to do this, or for your median filter it might be faster to use cwise.

Ah, right! I didn't see that, it's under "Special cases". So, it's:

ops.assign(dest, src) copies one array into another

Meaning you slice your source and destination arrays properly and then do assign().

This issue can be closed then.