I want to rebuild the following logic with numpy broadcasting function such as np.where: From a 2d array check per row if the first element satisfies a condition. If the condition is true then return the first three elements as a row, else the last three elements. A short MWE in form of a for-loop which I want to circumvent:
Tag: arrays
Code for updating the each element(binary) of array on the basis of majority voting of past 5 elements(including that element also)
I have an issue in writing a code for a problem related to the element updation of an array based on the majority voting of its past 5 elements including the element itself. Explanation: Suppose we have an array having elements in binary form. arr= [0 , 1, 0, 0, 0, 1, 0, 1 , 1, 1, 1, 0, 1,
Iteratively apply a function to an array
I want to create an array that contains g^0, g^1, g^2…. up to g^100 but all to mod50 (apologies if anyone knows how to format this so the powers are properly aligned I need a bit of help!) In my case g = 23, so I would want an array that looks like: I’ve included all my (incorrect) code at
Add in-between steps into array of numbers (Python)
I am looking for some function that takes an input array of numbers and adds steps (range) between these numbers. I need to specify the length of the output’s array. Example: Result: Is there something like that, in Numpy for example? I have a prototype of this function that uses dividing input array into pairs ([0,2], [2,5], [5,8]) and filling
UFuncTypeError: Cannot cast ufunc ‘det’ input from dtype(‘O’) to dtype(‘float64’) with casting rule ‘same_kind’? How to avoid this issue?
I’m trying to build a PDE in python. I’m new to this and wondering where I have gone wrong. Would appreciate some help. I understand that I have a python object and I’m trying to cast it to a float64 but is there any way around this? Here is my error Here is my code Answer A symbolic calculation like
Curve_Fit returrns error “Result from function Call is not a proper array of floats”
I am trying to call scipy curve_fit(), with the proper: model function xdata (float numpy 1D Array) ydata (float numpy 1D Array) p (float numpy 1D Array, initial values) However I am getting the error: ValueError: Object too deep for desired Array Result from function Call is not a proper array of floats. the model function I am computing is
Vectorized way to construct a block Hankel matrix in numpy (or scipy)
I want to contrsuct the following matrix : where each v(k) is a (ndarray) vector, say from a matrix Using a for loop, I can do something like this for example: And I get : Is there any way to construct this matrix in a vectorized way (which I imagine would be faster than for loops when it comes to
Compare values within a certain timeframe in arrays
I am trying to compare values (0’s and 1’s) in a array. I want to search for each “1” that appears in one column, for another “1” in the other column in a specific timeframe (for example, 5 seconds, 10 seconds, etc.). I will call the 1’s as “signals”. In example, I have an array such as: data1 = [
How would I perform an operation on each element in an array and add the new value to a new array in Python?
I have an array of ten random values generated using Numpy. I want to perform an operation on each element in the array by looping over all ten elements and then add the result of each to a new array. The first part of looping over the array but I am stuck on how to add the result to a
Why is this array changing when I’m not operating on it?
I have two arrays: And I’m running the foll)owing code: I get the following result: Why are the last elements changed? I don’t see why X is changed by indexing some elements. edit: added np.array Answer Output: