dpilger26 / NumCpp

C++ implementation of the Python Numpy library
https://dpilger26.github.io/NumCpp
MIT License
3.56k stars 555 forks source link

How to present a[ a [ : , 0] > 0 , : ] ? #128

Closed CNWindson closed 2 years ago

CNWindson commented 2 years ago

An ez question, wish your reply XD!

dpilger26 commented 2 years ago
a(a(a.rSlice(), 0) > 0, a.cSlice())
CNWindson commented 2 years ago
a(a(a.rSlice(), 0) > 0, a.cSlice())

Oops,it breaks an error like: no match 'operator>' (operand types are 'nc::NdArray' and 'int') @dpilger26

dpilger26 commented 2 years ago

Sorry, NumCpp doesn't currently have an operator() overload for a mask slice such as your question (I'll have to add that into the next release). The way you would need to do your example currently would be slightly more verbose where you actually extract the index values:

#include "NumCpp.hpp"

#include <cstdlib>
#include <iostream>

int main ()
{
    nc::random::seed(7);

    auto a = nc::random::randInt<int>({10, 10}, -10, 10);
    std::cout << a << '\n';

    auto b = a(nc::nonzero(a(a.rSlice(), 0) > 0).first, a.cSlice());
    std::cout << b;

    return EXIT_SUCCESS;
}

which outputs:

[[5, 8, -8, 7, -8, -9, 6, 8, -5, 4, ]
[5, 1, -3, -4, 6, -4, 9, 9, 7, -5, ]
[2, -5, -10, -10, -8, -7, -3, -4, 3, 2, ]
[0, -10, -5, 4, -2, 7, 3, -5, -7, 5, ]
[-8, 1, 1, -4, -10, 1, -4, -8, 3, 9, ]
[-7, 0, 1, 3, -5, -10, 4, -9, -5, -9, ]
[-9, 5, 0, -4, -6, -7, 3, -8, 5, -9, ]
[3, 0, 2, 5, -5, -1, -10, -8, -5, -9, ]
[3, -2, 3, -8, -3, 7, -10, 0, -6, -2, ]
[6, 3, -8, -8, 3, 8, -6, 9, -2, -9, ]]

[[5, 8, -8, 7, -8, -9, 6, 8, -5, 4, ]
[5, 1, -3, -4, 6, -4, 9, 9, 7, -5, ]
[2, -5, -10, -10, -8, -7, -3, -4, 3, 2, ]
[3, 0, 2, 5, -5, -1, -10, -8, -5, -9, ]
[3, -2, 3, -8, -3, 7, -10, 0, -6, -2, ]
[6, 3, -8, -8, 3, 8, -6, 9, -2, -9, ]]
CNWindson commented 2 years ago

@dpilger26 It really works, thx a lot! Although I couldn't understand the reason you said , it really helps a lot!