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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:22:45+00:00 2026-05-27T14:22:45+00:00

Say I have a 3D array, A(1:3,1:4,1:5) , and I only want to deal

  • 0

Say I have a 3D array, A(1:3,1:4,1:5), and I only want to deal with part of it, e.g.:

real :: A(1:3,1:4,1:5), B(1:5,1:2)
real, allocatable :: C(:,:)

allocate(C(size(A,1),size(B,2)))
C = matmul(A(1:3,1,1:5),B)

Fortran seems fine with that. However, if I needed to deal with the transpose, then the transpose function in Fortran gets confused, e.g.:

real :: A(1:3,1:4,1:5), B(1:3,1:2)
real, allocatable :: C(:,:)

allocate(C(size(A,3),size(B,2)))
C = matmul(transpose(A(1:3,1,1:5)),B)

How can I swap dimensions around in an array with Fortran? For example, I have A(3,4,5); is there a function/command that takes this and gives me A(5,4,3) or A(4,3,5) or any arrangement I could wish for? Without, of course, doing something like copying A to a dummy array with the dimensions in the order required. I’m looking for a simple one line elegant way.

Thank you.

  • 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-27T14:22:46+00:00Added an answer on May 27, 2026 at 2:22 pm

    You don’t have a problem with TRANSPOSE. It is working just fine for the sample code you provided. The problem is that your arrays are not compatible for matrix multiplication. From Fortran 2008 Standard draft:

    Case (i): If MATRIX A has shape [n, m] and MATRIX B has shape [m, k],
    the result has shape [n, k].

    In your case:

    C = matmul(transpose(A(1:3,1,1:5)),B)
    

    Here, transpose(A(1:3,1,1:5)) is a 5×3 matrix, and B is 2×5. Thus, these two matrices are non-comformable for MATMUL. I am wondering how you did not catch on this since compilers give a clear error message:

    gfortran 4.1.2:

    In file matrix.f90:13
    
    C = matmul(transpose(A(:,1,:)),B)
              1
    Error: different shape on dimension 2 for argument 'matrix_a' and dimension 1 for argument 'matrix_b' at (1) for intrinsic matmul
    

    ifort 12.0.2.137:

    matrix.f90(13): error #6241: The shapes of the arguments are inconsistent or nonconformable.   [MATMUL]
    C = matmul(transpose(A(:,1,:)),B)
    ----^
    compilation aborted for matrix.f90 (code 1)
    

    pgf90 10.6-0 compiles but produces a run-time error:

    0: MATMUL: nonconforming array shapes
    

    For reshaping arrays in Fortran, you can use intrinsic function RESHAPE. From Fortran 2008 Standard draft:

    13.7.140 RESHAPE (SOURCE, SHAPE [, PAD, ORDER])

    1 Description. Construct an array of an arbitrary shape.

    2 Class. Transformational
    function.

    3 Arguments. SOURCE shall be an array of any type. If PAD is
    absent or of size zero, the size of SOURCE shall be greater
    than or equal to PRODUCT (SHAPE). The size of the result is the product of the values of the
    elements of SHAPE. SHAPE shall be a rank-one integer array. SIZE (x), where x is the actual argument corresponding to
    SHAPE, shall be a constant expression whose value is positive and less than 16. It shall not have
    an element whose value is negative. PAD (optional) shall be an array of the same type and type parameters as SOURCE. ORDER (optional)
    shall be of type integer, shall have the same shape as SHAPE, and its
    value shall be a permutation of (1, 2, . . . , n), where n is the size
    of SHAPE. If absent, it is as if it were present with value (1, 2, . .
    . , n).

    4 Result Characteristics. The result is an array of shape
    SHAPE (that is, SHAPE (RESHAPE (SOURCE, SHAPE, PAD, ORDER)) is equal
    to SHAPE) with the same type and type parameters as SOURCE.

    5 Result
    Value. The elements of the result, taken in permuted subscript order
    ORDER (1), . . . , ORDER (n), are those of SOURCE in normal array
    element order followed if necessary by those of PAD in array element
    order, followed if necessary by additional copies of PAD in array
    element order.

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

Sidebar

Related Questions

Say I have an array of arbitrary size holding single characters. I want to
Say I have an array of table DDL queries and I want to get
Let's say I have an numpy array A of size n x m x
Say I have an array of items I want to split on (this is
Let's say I have an array of n elements(numbers or words), I want to
Let's say I have a string : {id:123,xCoord:01.234567,yCoord:89.012345,etc.:etcetc} I want to extract only the
Let's say I have this array [1,2,3,4,5,6,7,9] What I want to output is a
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',

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.