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 1064395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:56:03+00:00 2026-05-16T18:56:03+00:00

I am working with a 2D Numpy masked_array in Python. I need to change

  • 0

I am working with a 2D Numpy masked_array in Python.
I need to change the data values in the masked area such that they equal the nearest unmasked value.

NB. If there are more than one nearest unmasked values then it can take any of those nearest values (which ever one turns out to be easiest to code…)

e.g.

import numpy
import numpy.ma as ma

a = numpy.arange(100).reshape(10,10)
fill_value=-99
a[2:4,3:8] = fill_value
a[8,8] = fill_value
a = ma.masked_array(a,a==fill_value)

>>> a  [[0 1 2 3 4 5 6 7 8 9]
  [10 11 12 13 14 15 16 17 18 19]
  [20 21 22 -- -- -- -- -- 28 29]
  [30 31 32 -- -- -- -- -- 38 39]
  [40 41 42 43 44 45 46 47 48 49]
  [50 51 52 53 54 55 56 57 58 59]
  [60 61 62 63 64 65 66 67 68 69]
  [70 71 72 73 74 75 76 77 78 79]
  [80 81 82 83 84 85 86 87 -- 89]
  [90 91 92 93 94 95 96 97 98 99]],
  • I need it to look like this:
>>> a.data
 [[0 1 2 3 4 5 6 7 8 9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 ? 14 15 16 ? 28 29]
 [30 31 32 ? 44 45 46 ? 38 39]
 [40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 ? 89]
 [90 91 92 93 94 95 96 97 98 99]],

NB. where “?” could take any of the adjacent unmasked values.

What is the most efficient way to do this?

Thanks for your help.

  • 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-16T18:56:04+00:00Added an answer on May 16, 2026 at 6:56 pm

    You could use np.roll to make shifted copies of a, then use boolean logic on the masks to identify the spots to be filled in:

    import numpy as np
    import numpy.ma as ma
    
    a = np.arange(100).reshape(10,10)
    fill_value=-99
    a[2:4,3:8] = fill_value
    a[8,8] = fill_value
    a = ma.masked_array(a,a==fill_value)
    print(a)
    
    # [[0 1 2 3 4 5 6 7 8 9]
    #  [10 11 12 13 14 15 16 17 18 19]
    #  [20 21 22 -- -- -- -- -- 28 29]
    #  [30 31 32 -- -- -- -- -- 38 39]
    #  [40 41 42 43 44 45 46 47 48 49]
    #  [50 51 52 53 54 55 56 57 58 59]
    #  [60 61 62 63 64 65 66 67 68 69]
    #  [70 71 72 73 74 75 76 77 78 79]
    #  [80 81 82 83 84 85 86 87 -- 89]
    #  [90 91 92 93 94 95 96 97 98 99]]
    
    for shift in (-1,1):
        for axis in (0,1):        
            a_shifted=np.roll(a,shift=shift,axis=axis)
            idx=~a_shifted.mask * a.mask
            a[idx]=a_shifted[idx]
    
    print(a)
    
    # [[0 1 2 3 4 5 6 7 8 9]
    #  [10 11 12 13 14 15 16 17 18 19]
    #  [20 21 22 13 14 15 16 28 28 29]
    #  [30 31 32 43 44 45 46 47 38 39]
    #  [40 41 42 43 44 45 46 47 48 49]
    #  [50 51 52 53 54 55 56 57 58 59]
    #  [60 61 62 63 64 65 66 67 68 69]
    #  [70 71 72 73 74 75 76 77 78 79]
    #  [80 81 82 83 84 85 86 87 98 89]
    #  [90 91 92 93 94 95 96 97 98 99]]
    

    If you’d like to use a larger set of nearest neighbors, you could perhaps do something like this:

    neighbors=((0,1),(0,-1),(1,0),(-1,0),(1,1),(-1,1),(1,-1),(-1,-1),
               (0,2),(0,-2),(2,0),(-2,0))
    

    Note that the order of the elements in neighbors is important. You probably want to fill in missing values with the nearest neighbor, not just any neighbor. There’s probably a smarter way to generate the neighbors sequence, but I’m not seeing it at the moment.

    a_copy=a.copy()
    for hor_shift,vert_shift in neighbors:
        if not np.any(a.mask): break
        a_shifted=np.roll(a_copy,shift=hor_shift,axis=1)
        a_shifted=np.roll(a_shifted,shift=vert_shift,axis=0)
        idx=~a_shifted.mask*a.mask
        a[idx]=a_shifted[idx]
    

    Note that np.roll happily rolls the lower edge to the top, so a missing value at the top may be filled in by a value from the very bottom. If this is a problem, I’d have to think more about how to fix it. The obvious but not very clever solution would be to use if statements and feed the edges a different sequence of admissible neighbors…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've been working on a python project and reached the point that I
I am working on doing some digital filter work using Python and Numpy/Scipy. I'm
I'm working on some fairly computational intensive calculations that deal with numpy matrices and
Currently I am working on a python project that contains sub modules and uses
I am working with the boost Python and NumPy API. I know on the
I have a working python 2.7 program that calls a DLL. I am trying
Working with an undisclosed API, I found a function that can set the number
I'm working in an embedded Linux environment and I have some Python code which
I'm working on an application in Clojure that needs to multiply large matrices and
I have been working on a program where I need to slowly and smoothly

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.