Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

handling 2 dim array

bhpark70rda_go_kr
742 Views
I have 3 by 3 arrary(xpy).

I want to select (2,1), (3,2) element from the arrary.

I know the position of row and columnas followingrw = (/2, 3/) and cl = (/1,2/).

So I want to select cross point of rw and cl arrays.

How can I select?
0 Kudos
5 Replies
Yuan_C_Intel
Employee
742 Views

Do you mean following?

Do i=1, 2
xpy(rw(i), cl(i)) = ...

0 Kudos
bhpark70rda_go_kr
742 Views
Actually, I want to store the result of matrix multiplication into the specific elements of 2 dim array.

for example

I have the result of 2 by 2 matrix and 2 by 1 vector multiplication and want to store it into (2,1) (3,2) of 3 by 3 matrix at one time.

Is it possible?
0 Kudos
Jugoslav_Dujic
Valued Contributor II
742 Views
Actually, I want to store the result of matrix multiplication into the specific elements of 2 dim array.

for example

I have the result of 2 by 2 matrix and 2 by 1 vector multiplication and want to store it into (2,1) (3,2) of 3 by 3 matrix at one time.

Is it possible?

No, it's not possible in the single line.

For 1-D case, you may use an index (order) array, for example:

real:: A(5) = (/1., 2., 3., 4., 5.,/)
integer:: iOrder(2) = (/2, 5/)

A(iOrder) = 0. !gives 1, 0, 3, 4, 0

But there's no equivalent construct for 2-D case -- you may not use a 2-d array as an index array. You have to use a DO-loop, as Yolanda outlined.
0 Kudos
IanH
Honored Contributor III
742 Views
Quoting - Jugoslav Dujic
You have to use a DO-loop, as Yolanda outlined.

Or, for extra obfuscation points and a possible saving of two keystrokes ;)

FORALL (i=1:2) xpy(rw(i),cl(i)) = the_source(i)


0 Kudos
Yuan_C_Intel
Employee
742 Views
Quoting - Jugoslav Dujic
But there's no equivalent construct for 2-D case -- you may not use a 2-d array as an index array. You have to use a DO-loop, as Yolanda outlined.

Fortran 90/95 (2003 has more extensions)do providea special intrinsic function, callled RESHAPE, that changes the shape of an array,which can initialize a 2-D array in a single assignment, the form of the RESHAPE function is:
output = RESHAPE ( array1, array2)
where array1 contains the data to reshape and array2 is a rank-1 array describing the new shape. The number of elements in array2 is the number of dimensions in the output array, and the value of each element in array2 is the extent of each dimenstion. The number of elements in array 1 must be the same as the number of elements in the shape specified in array2, or the RESHAPE function will fail.

As for bhpark70rda.go.kr's case,we can have:
integer, dimension(3,3):: xpy
xpy = RESHAPE ( /11, *, 31, 12, 22, *, 13, 23, 33/, /3,3/)
Where *are the position of (2,1) (3,2) you put your result into.

0 Kudos
Reply