maths of arrays


Taiseer
01-13-2011, 02:36 PM
Dear all,

I have one matrix of 450*550, let us say matrix_a
I want to generate a new matrix (let is say matrix_b, off course 450*550). The elements of this matrix (matrix_b) has all the same value (let us say 1.5).

Now I want to generate a third matrix whihc is matrix_c = matrix_a + matrix_b.

Thanks
Taiseer

davekw7x
01-15-2011, 09:44 AM
...matrix_c = matrix_a + matrix_b
In Fortran 90 matrix arithmetic operations are carried out element-by-element. Just add them with a normal-looking arithmetic expression.

PROGRAM MatAdd

IMPLICIT NONE

INTEGER i, j
REAL a(2,3), b(2,3), c(2,3)

a(1,:) = (/ 1.0, 2.0, 3.0 /)
a(2,:) = (/ 4.0, 5.0, 6.0 /)

b(1,:) = (/ 7.0, 8.0, 9.0 /)
b(2,:) = (/10.0, 11.0, 12.0 /)

c = a + b ! Yes! That's all it takes to add the matrices. Really.

do i = 1, 2
do j = 1, 3
write(*, '("a(",I0,",",I0,") = ", f4.1)') i, j, a(i,j)
end do
end do
write(*,*)


do i = 1, 2
do j = 1, 3
write(*, '("b(",I0,",",I0,") = ", f4.1)') i, j, b(i,j)
end do
end do
write(*,*)

do i = 1, 2
do j = 1, 3
write(*, '("c(",I0,",",I0,") = ", f4.1)') i, j, c(i,j)
end do
end do

END PROGRAM MatAdd


Output:
a(1,1) = 1.0
a(1,2) = 2.0
a(1,3) = 3.0
a(2,1) = 4.0
a(2,2) = 5.0
a(2,3) = 6.0

b(1,1) = 7.0
b(1,2) = 8.0
b(1,3) = 9.0
b(2,1) = 10.0
b(2,2) = 11.0
b(2,3) = 12.0

c(1,1) = 8.0
c(1,2) = 10.0
c(1,3) = 12.0
c(2,1) = 14.0
c(2,2) = 16.0
c(2,3) = 18.0



Regards,

Dave