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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:49:09+00:00 2026-06-18T06:49:09+00:00

In python (using numpy), I can broadcast an array to a different shape: >>>

  • 0

In python (using numpy), I can broadcast an array to a different shape:

>>> import numpy as np
>>> a = np.array([2,3,4])
>>> b = np.zeros((3,2))
>>> b[:,:] = np.zeros((3,2))
>>> b[:,:] = a[:,np.newaxis]  #<-- np.newaxis allows `a` to be "broadcasted" to the same shape as b.
>>> b
array([[ 2.,  2.],
       [ 3.,  3.],
       [ 4.,  4.]])
>>> c = np.zeros((2,3))
>>> c[:,:] = a[np.newaxis,:]
>>> c
array([[ 2.,  3.,  4.],
       [ 2.,  3.,  4.]])

Is there any way to achieve the same effect in fortran? I have a subroutine which expects a 2D array to be passed in — I would like to “broadcast” my 1-D arrays up to 2-D as I’ve demonstrated above. As it seems that it is likely to matter, my 2D array does have an explicit interface.

As a side note, I thought that this functionality might be provided by the reshape intrinsic, — Something like:

real,dimension(3) :: arr1d
reshape(arr1d, (/3,3/), order=(/1,/1))

but after reading the docs, I don’t think that this is possible since order seems to need to include all the numbers 1 to “N”.

Edit: To be a little more clear, I’m looking for a simply way to create a couple of transforms on an input a such that:

case 1

b(i,j) .eq. a(i)  !for all j, or even just j=1,2

and

case 2

b(j,i) .eq. a(i)  !for all j, or even just j=1,2

bonus points1 for arbitrary dimensionality:

b(i,j,k) .eq. a(i,j)
b(i,k,j) .eq. a(i,j)

etc.

1disclaimer — I don’t actually have SO super powers to bestow bonus points upon the answerer 😉

  • 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-06-18T06:49:10+00:00Added an answer on June 18, 2026 at 6:49 am

    I’m not sure what you are trying to accomplish but here are a couple of fragments which may help.

    reshape can take an optional argument, called pad, which can be used to provide the ‘extra’ elements needed when you reshape into an array with more elements than you started with, say from 3×4 to 2x4x2.

    You may also be interested in the spread function which is designed for ‘upranking’ arrays, that is taking a rank-N array in and putting out a rank-N+1 array. The fragment in your second copy could be rewritten as

    array2d = spread(array1d,2,2)
    

    In this example the second argument is the dimension along which to spread the first argument to make the output. The third argument is the number of copies of the input array to make.

    PS The call to spread should perhaps be spread(array1d,1,2), I haven’t checked it.

    EDIT in response to OP’s editing of question

    The two cases, 1 and 2, are satisfied by spreading across dimensions 2 and 1 respectively. In Fortran

    b = spread(a,2,j)
    

    and

    b = spread(a,1,j)
    

    Since spread returns an array with rank 1 greater than the rank of its first argument, it provides the sought-for arbitrary dimensionality. However, since it’s so space-consuming to show arrays of rank-3 and above I’m not going to.

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

Sidebar

Related Questions

Can anyone help with a snippet of code using numpy and python? Given an
Using Python NumPy to calculate the sum of the column of a matrix: import
I am using Python 2.7 and opencv 2.1. I have a numpy array and
I have the following code in Python using Numpy: p = np.diag(1.0 / np.array(x))
I created a python .py with some sort methods. I'm using numpy so that
I am using Python's multiprocessing module to process large numpy arrays in parallel. The
I'm coping data in python using OpenCL onto my graphic card. There I've a
I am relatively new to python. I have a numpy array that has 3
Using Python 2.7.3 with Numpy 1.6.2 on a 64-bit Ubuntu 12.04. Additional versions are
I'm using numpy.delete to remove elements from an array that is inside a while

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.