Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:51:47+00:00 2026-05-22T16:51:47+00:00

I have a numpy array of floats and I wish to recalculate new values

  • 0

I have a numpy array of floats and I wish to recalculate new values using a formula that depends on the column being recalculated.

I have initially tried to loop over the columns, masking the array except the column to be recalculated, and the replacing the values with the new ones with numpy.putmask but this does not retain the order, as it attempts to place a value in each element and failing that tries with the next calculated value on the next element, à la:

>>> import numpy as np

>>> x = [[  1.,   2.],
        [  3.,   4.],
        [  5.,   6.],
        [  7.,   8.],
        [  9.,  10.]]
>>> mask = [[ True, False],
           [ True, False],
           [ True, False],
           [ True, False],
           [ True, False]]
>>> y = [ 21.,  22.,  23.,  24.,  25.]
>>> np.putmask(x,mask,y)
>>> print x
[[ 21.   2.]
[ 23.   4.]
[ 25.   6.]
[ 22.   8.]
[ 24.  10.]]

I need a solution that will retry with the same value until it finds a True value, such that x would look like:

[[ 21.   2.]
[ 22.   4.]
[ 23.   6.]
[ 24.   8.]
[ 25.  10.]]

Any solutions or other methods welcomed. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:51:48+00:00Added an answer on May 22, 2026 at 4:51 pm

    putmask(x,mask,y) sets x.flat[n] = y[n] for each n where mask.flat[n] is True.

    In [17]: list(x.flat)
    Out[17]: [21.0, 2.0, 22.0, 4.0, 23.0, 6.0, 24.0, 8.0, 25.0, 10.0]
    
    In [18]: list(mask.flat)
    Out[18]: [True, False, True, False, True, False, True, False, True, False]
    

    Since mask.flat alternates between True and False, you end up setting every other value in x.flat with every other value in y.

    Since y is not the same size as x, the values in y are repeated. This is what leads to the (undesired) alternating values you see in x after calling
    putmask(x,mask,y).

    If instead you wish to assign new values to x wherever mask is True,
    then all you need is assignment with numpy indexing:

    x[mask]=y
    

    For example,

    import numpy as np
    x = np.array([[  1.,   2.],
            [  3.,   4.],
            [  5.,   6.],
            [  7.,   8.],
            [  9.,  10.]])
    mask = np.array([[ True, False],
               [ True, False],
               [ True, False],
               [ True, False],
               [ True, False]])
    y = np.array([ 21.,  22.,  23.,  24.,  25.])
    x[mask]=y
    print(x)
    # [[ 21.   2.]
    #  [ 22.   4.]
    #  [ 23.   6.]
    #  [ 24.   8.]
    #  [ 25.  10.]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an numpy array which I save to a image using savefig(). Then
I have a numpy array with positive and negative values in. a = array([1,1,-1,-2,-3,4,5])
I have a 1D numpy array, and some offset/length values. I would like to
I have a Numpy array that looks like >>> a array([[ 3. , 2.
I have a numpy array called landuse that's a series of numbers 1-3 representing
I have a 2D numpy array. Some of the values in this array are
I have a numpy array A such that A.shape[axis] = n+1. Now I want
I am using Python 2.7 and opencv 2.1. I have a numpy array and
I have a Numpy array and a list of indices whose values I would
I have a numpy array data that just contains a set of integer counts.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.