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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:17:35+00:00 2026-05-25T22:17:35+00:00

I was trying this simple line of assigning codes to a structured array in

  • 0

I was trying this simple line of assigning codes to a structured array in numpy, I am not quiet sure, but something wrong happens when I assign a matrix to a sub_array in a structured array I created as follows:

new_type = np.dtype('a3,(2,2)u2')
x = np.zeros(5,dtype=new_type)
x[1]['f1'] = np.array([[1,1],[1,1]])
print x
Out[143]: 
array([('', [[0, 0], [0, 0]]), ('', [[1, 0], [0, 0]]),
   ('', [[0, 0], [0, 0]]), ('', [[0, 0], [0, 0]]),
   ('', [[0, 0], [0, 0]])], 
  dtype=[('f0', '|S3'), ('f1', '<u2', (2, 2))])

Shouldn’t the second field of the subarray equals at this stage

[[1,1],[1,1]]
  • 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-25T22:17:35+00:00Added an answer on May 25, 2026 at 10:17 pm

    I think you want to set things slightly differently. Try:

    x['f1'][1] = np.array([[1,1],[1,1]])
    

    which results in:

    In [43]: x = np.zeros(5,dtype=new_type)
    
    In [44]: x['f1'][1] = np.array([[1,1],[1,1]])
    
    In [45]: x
    Out[45]: 
    array([('', [[0, 0], [0, 0]]), ('', [[1, 1], [1, 1]]),
           ('', [[0, 0], [0, 0]]), ('', [[0, 0], [0, 0]]),
           ('', [[0, 0], [0, 0]])], 
          dtype=[('f0', '|S3'), ('f1', '<u2', (2, 2))])
    

    This is not to say that this isn’t strange behavior though since both x['f1'][1] and x[1]['f1'] print the same results, but clearly are different:

    In [51]: x['f1'][1]
    Out[51]: 
    array([[1, 1],
           [1, 1]], dtype=uint16)
    
    In [52]: x[1]['f1'] 
    Out[52]: 
    array([[1, 1],
           [1, 1]], dtype=uint16)
    
    In [53]: x[1]['f1'] = 2
    
    In [54]: x
    Out[54]: 
    array([('', [[0, 0], [0, 0]]), ('', [[2, 1], [1, 1]]),
           ('', [[0, 0], [0, 0]]), ('', [[0, 0], [0, 0]]),
           ('', [[0, 0], [0, 0]])], 
          dtype=[('f0', '|S3'), ('f1', '<u2', (2, 2))])
    
    In [55]: x['f1'][1] = 3
    
    In [56]: x
    Out[56]: 
    array([('', [[0, 0], [0, 0]]), ('', [[3, 3], [3, 3]]),
           ('', [[0, 0], [0, 0]]), ('', [[0, 0], [0, 0]]),
           ('', [[0, 0], [0, 0]])], 
          dtype=[('f0', '|S3'), ('f1', '<u2', (2, 2))])
    

    I’d have to think about it a bit more to figure out exactly what is going on.

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

Sidebar

Related Questions

I'm trying to get this simple PowerShell script working, but I think something is
I am trying to learn the nuances of this simple function but am not
I'm trying to get this simple program to work on windows, but it crashes:
This is a simple htaccess question for experts but I have been trying to
i'm trying to do something pretty simple: line = name : bob k, v
I am trying to understand this simple hashlib code in Python that has been
I am trying to create this simple application in c#: when the user double
i am trying to compile this very simple piece of code class myList {
I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I'm trying to get the hang of jQuery and I had this simple bit

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.