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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:01:47+00:00 2026-05-20T14:01:47+00:00

I have a NxN matrix which I want to split into non-overlap KxK block.

  • 0

I have a NxN matrix which I want to split into non-overlap KxK block. For each block, I want to assign new values to the elements.

Since this looks like a good place for a generator, I implemented:

def extracted_patches(im, top_left, patch_size, grid_size):
    '''Extract patches in row-major order following a specific configuration

    Parameters
    ----------
    im : the input image (2D numpy array)
    top_left : (y,x) coordinate of the top left point (e.g. (3,5))
    grid_size : (cy, cx) how many patches in the y-direction and in the x-direction
    patch_size : (h, w) how many pixels for the size of each patch

    Returns
    -------
    a generator that goes through each patch (a numpy array view) in row-major order
    '''
    for i in xrange(grid_size[0]):
        for j in xrange(grid_size[1]):
            yield im[top_left[0] + patch_size[0]*i : top_left[0] + patch_size[0]*(i+1)
                    ,top_left[1] + patch_size[1]*j : top_left[1] + patch_size[1]*(j+1)]

Then when I try to change the value of each patch, the assignment change the variable value instead of the value the generator gives

output_im = np.zeros((patch_size[0]*grid_size[0], patch_size[1]*grid_size[1]))        
output_im_it = extracted_patches(output_im, (0,0), patch_size, grid_size)

for i in xrange(grid_size[0]*grid_size[1]):
    output_im_it = np.random.random(patch_size)

Can my generator be mutable?

  • 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-20T14:01:48+00:00Added an answer on May 20, 2026 at 2:01 pm

    As with any variables holding a numpy array, to change the value “pointed to” you want to avoid assigning to the variable but assign to a slice of it. Try this:

    for submat in output_im_it:
         submat[:] = np.random.random(patch_size)
    

    As a response to your edit: it seems you have confused the generator object with the values it yields. You can’t assign to slices of the generator object itself. You can assign to slices of the numpy arrays, which you can get with e.g. output_im_it.next() or with a for loop, as above.

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

Sidebar

Related Questions

I have a NxN grid with some values, which change every time step. I
I have a NxM matrix and I want to compute the NxN matrix of
I have an NxN matrix filled with zeros. Now I want to add to
Suppose you have an NxN matrix and you have to check whether it's a
I wish to populate an nxn matrix with vectors. I have the following: [x
I have a large matrix from which I would like to gather a collection
I want to write a function to create an empty square matrix have size
Suppose you have an NxN grid, and you want to visit every cell exactly
Suppose I have a NxN matrix M (lil_matrix or csr_matrix) from scipy.sparse, and I
I have a NxN grid of values and a 2xM list of x,y data

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.