Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Advanced Array Indexing #89

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

Closed
DecemberDream opened this issue Nov 29, 2021 · 2 comments
Closed

Advanced Array Indexing #89

DecemberDream opened this issue Nov 29, 2021 · 2 comments

Comments

@DecemberDream
Copy link

Is advanced array indexing notation available? I tried implementing the second example from the NumPy documentation about array indexing, however, each time I try to run it, I get the following error:

System.ArgumentException: 'Invalid slice notation'

My code is:

NDarray test = np.array(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } });
NDarray rows = np.array(new int[,] { { 0, 0 }, { 3, 3 } });
NDarray cols = np.array(new int[,] { { 0, 2 }, { 0, 2 } });
printNDarray2D(test[$"{rows}, {cols}"]); // System.ArgumentException: 'Invalid slice notation'
// should return
// [[0, 2],
//  [9, 11]]

Where the printNDarray2D-function is:

public static void printNDarray2D(NDarray array)
{
    for (int i = 0; i < array.len; ++i)
    {
        for(int j = 0; j < array[0].len; ++j)
        {
            Console.Write("{0,10} ", ((double)array[i][j]).ToString("0.00000"));
        }

        Console.WriteLine();
    }

    Console.WriteLine();
}

Is there a way to index an NDarray using another NDarray? Am I missing something?

henon added a commit that referenced this issue Dec 1, 2021
@henon
Copy link
Contributor

henon commented Dec 1, 2021

yes:

        [TestMethod]
        public void IssueByDecemberDream2()
        {
            NDarray test = np.array(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } });
            NDarray rows = np.array(new int[,] { { 0, 0 }, { 3, 3 } });
            NDarray cols = np.array(new int[,] { { 0, 2 }, { 0, 2 } });

            var b = test[rows, cols];
            // should return
            // [[0, 2],
            //  [9, 11]]
            Assert.AreEqual("array([[ 0,  2],\n       [ 9, 11]])", b.repr);
        }

@henon henon closed this as completed Dec 1, 2021
@JimXu1989
Copy link

JimXu1989 commented Feb 3, 2022

The code only return a single value, b = {0}, not a matrix.

Edit by Henon: this has been moved to issue #95

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