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

ndarray not getting value as expected. #37

Open
softmarshmallow opened this issue Mar 28, 2018 · 2 comments
Open

ndarray not getting value as expected. #37

softmarshmallow opened this issue Mar 28, 2018 · 2 comments

Comments

@softmarshmallow
Copy link

im trying to create a list with some specific rules.
ex ) x :2, y:5
0, 0
0, 1
0, 2
0, 3
0, 4
1, 0
1, 1
1, 2
1, 3
1, 4

it should have 2 sets, y should be repeated for x times.
so the table shape should be [2, x * y].
1D data should look like [0, 1, 0, 2, 0, 3, 0, 4, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4]

it works fine until here but when i try to get myNdarray.get(0, 5), i expected 1 but gives 2 instead.

    let x = 2;
    let y = 4;

    let datArr = []
    for (let xx = 0; xx < x; xx++){
        for (let yy = 0; yy < y; yy++){
            datArr.push(xx)
            datArr.push(yy)
            // narr.
        }
    }
    let narr = ndarray(new Float32Array(datArr), [2, x*y]);
    console.log(narr)
    console.log(narr.get(0, 5))
    console.log(narr.get(1, 5))

here is my test code, and

this is the log

NdArrayTest.js:35 View2dfloat32 {data: Float32Array(20), shape: Array(2), stride: Array(2), offset: 0}data: Float32Array(20) [0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4]offset: 0shape: (2) [2, 10]stride: (2) [10, 1]order: (...)size: (...)proto: Object
NdArrayTest.js:36 2
NdArrayTest.js:37 2

what is happening?
am i using the wrong code?

@rreusser
Copy link
Member

rreusser commented Mar 28, 2018

The first thing that jumps out is the possibility of mistakes when making assumptions about the 1D data buffer. Can you use .set() instead and examine narr.data to see what it looks like instead of passing the data buffer directly? It's very slightly slower, but for the majority cases is equivalent and at least much less error-prone.

@wangzhuwei212003
Copy link

First,if you want x = 2, y = 5, the value of y in you code should be 5 not 4.
If

[
  [0, 0, 0, 1, 0, 2, 0, 3, 0, 4],
  [1, 0, 1, 1, 1, 2, 1, 3, 1, 4]
]

is the normal array you want, then it is.

narr.get(0, 5) is 2, I dont know why you expect 1 ? narr.get(1, 5) is also 2.

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

No branches or pull requests

3 participants