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

The Archive Base Latest Questions

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

I have the following array in NumPy: A = array([1, 2, 3]) How can

  • 0

I have the following array in NumPy:

A = array([1, 2, 3])

How can I obtain the following matrices (without an explicit loop)?

B = [ 1 1 1
      2 2 2
      3 3 3 ]

C = [ 1 2 3
      1 2 3
      1 2 3 ]

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-16T16:55:52+00:00Added an answer on May 16, 2026 at 4:55 pm

    Edit2: The OP asks in the comments how to compute

    n(i, j) = l(i, i) + l(j, j) - 2 * l(i, j)
    

    I can think of two ways. I like this way because it generalizes easily:

    import numpy as np
    
    l=np.arange(9).reshape(3,3)
    print(l)
    # [[0 1 2]
    #  [3 4 5]
    #  [6 7 8]]
    

    The idea is to use np.ogrid. This defines a list of two numpy arrays, one of shape (3,1) and one of shape (1,3):

    grid=np.ogrid[0:3,0:3]
    print(grid)
    # [array([[0],
    #        [1],
    #        [2]]), array([[0, 1, 2]])]
    

    grid[0] can be used as a proxy for the index i, and
    grid[1] can be used as a proxy for the index j.

    So everywhere in the expression l(i, i) + l(j, j) - 2 * l(i, j), you simply replace i–>grid[0], and j–>grid[1], and numpy broadcasting takes care of the rest:

    n=l[grid[0],grid[0]] + l[grid[1],grid[1]] + 2*l
    print(n)
    # [[ 0  6 12]
    #  [10 16 22]
    #  [20 26 32]]
    

    However, in this particular case, since l(i,i) and l(j,j) are just the diagonal elements of l, you could do this instead:

    d=np.diag(l)
    print(d)
    # [0 4 8]
    

    d[np.newaxis,:] pumps up the shape of d to (1,3), and
    d[:,np.newaxis] pumps up the shape of d to (3,1).

    Numpy broadcasting pumps up d[np.newaxis,:] and d[:,np.newaxis] to shape (3,3), copying values as appropriate.

    n=d[np.newaxis,:] + d[:,np.newaxis] + 2*l
    print(n)
    # [[ 0  6 12]
    #  [10 16 22]
    #  [20 26 32]]
    

    Edit1: Usually you do not need to form B or C. The purpose of Numpy broadcasting is to allow you to use A in place of B or C. If you show us how you plan to use B or C, we might be able to show you how to do the same with A and numpy broadcasting.


    (Original answer):

    In [11]: B=A.repeat(3).reshape(3,3)
    
    In [12]: B
    Out[12]: 
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]])
    
    In [13]: C=B.T
    
    In [14]: C
    Out[14]: 
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    

    or

    In [25]: C=np.tile(A,(3,1))
    
    In [26]: C
    Out[26]: 
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    
    In [27]: B=C.T
    
    In [28]: B
    Out[28]: 
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]])
    

    From the dirty tricks department:

    In [57]: np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(4,0))
    Out[57]: 
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]])
    
    In [58]: np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(0,4))
    Out[58]: 
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    

    But note that these are views of A, not copies (as were the solutions above). Changing B, alters A:

    In [59]: B=np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(4,0))
    
    In [60]: B[0,0]=100
    
    In [61]: A
    Out[61]: array([100,   2,   3])
    

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

Sidebar

Related Questions

i have a numpy array like the following [[ 1 2 3 4 ]
I have the following numpy array: # A B C Y my_arr = np.array([
I have the following 3 x 3 x 3 numpy array called a (the
I have the following range of numpy data (deltas of usec timestamps): array([ 4.312,
I have a NumPy array a like the following: >>> str(a) '[ nan nan
If I have the following matrix: import numpy ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
I have a numpy structured array of the following form: x = np.array([(1,2,3)]*2, [('t',
I have the following array: >>> x = numpy.array([2,4,2,3,1]) >>> x array([2, 4, 2,
I have the following code in Python using Numpy: p = np.diag(1.0 / np.array(x))
Suppose I have the following numpy arrays: >>a array([[0, 0, 2], [2, 0, 1],

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.