NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
extract.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <vector>
31 
33 #include "NumCpp/NdArray.hpp"
34 
35 namespace nc
36 {
37  //============================================================================
38  // Method Description:
47  template<typename dtype>
48  NdArray<dtype> extract(const NdArray<bool>& condition, const NdArray<dtype>& arr)
49  {
50  if (condition.size() != arr.size())
51  {
52  THROW_INVALID_ARGUMENT_ERROR("Input arguments 'condition' and 'arr' must have the same size.");
53  }
54 
55  std::vector<dtype> values;
56  for (decltype(arr.size()) i = 0; i < arr.size(); ++i)
57  {
58  if (condition[i])
59  {
60  values.push_back(arr[i]);
61  }
62  }
63 
64  return NdArray<dtype>(values.begin(), values.end());
65  }
66 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
Definition: Coordinate.hpp:45
NdArray< dtype > extract(const NdArray< bool > &condition, const NdArray< dtype > &arr)
Definition: extract.hpp:48