question about assigning values to a matrix - Fortran

This is a discussion on question about assigning values to a matrix - Fortran ; Hi guys, I have a simple question to ask. In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2). Then I want to assign value to this matrix. But when I use mat=(/ 1,2,3,4 /), it says "Error: The ...

+ Reply to Thread
Results 1 to 5 of 5

question about assigning values to a matrix

  1. Default question about assigning values to a matrix

    Hi guys,

    I have a simple question to ask.

    In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2).
    Then I want to assign value to this matrix. But when I use mat=(/
    1,2,3,4 /), it says "Error: The shapes of the array expressions do not
    conform." Do you know how should I modify it? Thanks a lot.

    John

  2. Default Re: question about assigning values to a matrix

    Hello,

    On 2008-07-08 15:33:30 -0400, John <john131411@yahoo.com> said:

    > Hi guys,
    >
    > I have a simple question to ask.
    >
    > In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2).
    > Then I want to assign value to this matrix. But when I use mat=(/
    > 1,2,3,4 /), it says "Error: The shapes of the array expressions do not
    > conform." Do you know how should I modify it? Thanks a lot.


    mat is rank-2, the array constructor is always rank-1.
    Use reshape() to make the conversion.

    HTH

    >
    > John



    --
    Cheers!

    Dan Nagle


  3. Default Re: question about assigning values to a matrix

    In article <2008070815364175249-dannagle@verizonnet>,
    Dan Nagle <dannagle@verizon.net> wrote:

    > Hello,
    >
    > On 2008-07-08 15:33:30 -0400, John <john131411@yahoo.com> said:
    >
    > > Hi guys,
    > >
    > > I have a simple question to ask.
    > >
    > > In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2).
    > > Then I want to assign value to this matrix. But when I use mat=(/
    > > 1,2,3,4 /), it says "Error: The shapes of the array expressions do not
    > > conform." Do you know how should I modify it? Thanks a lot.

    >
    > mat is rank-2, the array constructor is always rank-1.
    > Use reshape() to make the conversion.


    The common idiom is

    mat = reshape( (/1,2,3,4 /), shape(mat) )

    This avoids the redundancy of having to specify the dimensions in
    multiple places in the program. Somewhat surprisingly, and
    conveniently, this works for parameter declaration assignments too.

    However, I don't know of a way to do this for components of derived
    types. For these, I think you have to specify the last argument in
    the (usually) redundant way.


    $.02 -Ron Shepard

  4. Default Re: question about assigning values to a matrix

    On Jul 8, 11:27 pm, Ron Shepard <ron-shep...@NOSPAM.comcast.net>
    wrote:
    > In article <2008070815364175249-dannagle@verizonnet>,
    >  Dan Nagle <danna...@verizon.net> wrote:
    >
    > > Hello,

    >
    > > On 2008-07-08 15:33:30 -0400, John <john131...@yahoo.com> said:

    >
    > > > Hi guys,

    >
    > > > I have a simple question to ask.

    >
    > > > In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2)..
    > > > Then I want to assign value to this matrix.  But when I use mat=(/
    > > > 1,2,3,4 /), it says "Error: The shapes of the array expressions do not
    > > > conform." Do you know how should I modify it? Thanks a lot.

    >
    > > mat is rank-2, the array constructor is always rank-1.
    > > Use reshape() to make the conversion.

    >
    > The common idiom is
    >
    >    mat = reshape( (/1,2,3,4 /), shape(mat) )
    >
    > This avoids the redundancy of having to specify the dimensions in
    > multiple places in the program.  Somewhat surprisingly, and
    > conveniently, this works for parameter declaration assignments too.
    >
    > However, I don't know of a way to do this for components of derived
    > types.  For these, I think you have to specify the last argument in
    > the (usually) redundant way.
    >
    > $.02 -Ron Shepard


    Got it. Thanks very much!

  5. Default Re: question about assigning values to a matrix

    On Jul 8, 3:36 pm, Dan Nagle <danna...@verizon.net> wrote:
    > Hello,
    >
    > On 2008-07-08 15:33:30 -0400, John <john131...@yahoo.com> said:
    >
    > > Hi guys,

    >
    > > I have a simple question to ask.

    >
    > > In fortran90, I define a 2by2 matrix, for example, real(8):: mat(2,2).
    > > Then I want to assign value to this matrix.  But when I use mat=(/
    > > 1,2,3,4 /), it says "Error: The shapes of the array expressions do not
    > > conform." Do you know how should I modify it? Thanks a lot.

    >
    > mat is rank-2, the array constructor is always rank-1.
    > Use reshape() to make the conversion.
    >
    > HTH
    >
    >
    >
    > > John

    >
    > --
    > Cheers!
    >
    > Dan Nagle


    Many thanks! Have a good evening!
    John

+ Reply to Thread