marco
marco

Reputation: 975

How to convert simple array into two-dimensional array(matrix) in Fortran?

I had to read data from a file and store all data in a one dimensional array. However, Some data I have to store in a matrix (2 dimensional array) How Can I do this?

For example if my data is 1x7 array [1,2,3,1,5,2,8] and the first to 6th belong to a matrix 2x3 how can I store in a new array variable?

Upvotes: 1

Views: 2400

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78306

Suppose that your 7-element array is called array7, then the following expression should return a 2x3 array containing the first 6 elements of array7

reshape(array7(1:6),[2,3])

If that puts the elements into the new array in the wrong order, try

reshape(array7(1:6),[2,3],order=[2,1])

Note that I've used a named optional argument in the second version, there is another optional argument (pad) which would be, by default, the 3rd argument to reshape.

Upvotes: 4

Related Questions