如何将矩阵插入fortran程序?我有一个脚本:
program matrix
implicit none
integer, parameter :: size = 20
real, dimension(1:size,1:size) :: M1, M2, M
integer :: a, b, c, i, j
print *,'Insert size of matrix: a x b, b x c'
read(*,*) a, b, c
print *, 'Insert matrix 1'
do i = 1, a
read(*,*) (M1(i,j), j=1,b)
end do
print *, M1
end program
运行程序时,我得到以下信息:
Insert size of matrix: a x b, b x c
2
2
2
Insert matrix 1
1
2
3
0
1.00000000 3.00000000 -9.23812943E+22 4.56206728E-41 1.40129846E-45 0.00000000 -7.45794612E+22 4.56206728E-41 -9.23928235E+22 4.56206728E-41 0.00000000 4.56206728E-41 -3.43290277E-17 4.59163468E-41 -7.46993155E+22 4.56206728E-41 2.80110774E-37 0.00000000 -9.23812943E+22 4.56206728E-41 2.00000000 0.00000000 -7.45794612E+22 4.56206728E-41 -9.23928235E+22 4.56206728E-41 0.00000000 4.56206728E-41 -3.43292395E-17 4.59163468E-41 -3.78086916E+22 4.56206728E-41 1.02033236E-38 0.00000000 -5.63054167E+22 4.56206728E-41 -5.61578697E+22 4.56206728E-41 -3.76465891E+22 4.56206728E-41 2.80259693E-45 4.56206728E-41 0.00000000 4.56206728E-41 -3.43294513E-17 4.59163468E-41 4.20389539E-45 4.56206728E-41 -3.43293983E-17 4.59163468E-41 0.00000000 4.56206728E-41 -9.24416786E+22 4.56206728E-41 0.00000000 0.00000000 9.80908925E-45 0.00000000 -9.23928235E+22 4.56206728E-41 -3.43296630E-17 4.59163468E-41 3.28687239E-22 0.00000000 -9.23890045E+22 4.56206728E-41 -3.43299542E-17 4.59163468E-41 -9.24453535E+22 4.56206728E-41 -9.23812943E+22 4.56206728E-41 0.00000000 0.00000000 -7.45824156E+22 4.56206728E-41 2.80259693E-45 0.00000000 -9.24453535E+22 4.56206728E-41 1.40129846E-45 0.00000000 0.00000000 0.00000000 1.40129846E-45 0.00000000 -9.23812943E+22 4.56206728E-41 -5.63054167E+22 4.56206728E-41 -7.45824156E+22 1.40129846E-45 -3.43293983E-17 4.59163468E-41 -3.43294513E-17 4.59163468E-41 -9.23890045E+22 4.56206728E-41 0.00000000 0.00000000 1.40129846E-45 0.00000000 -9.23812943E+22 4.56206728E-41 NaN 0.00000000 -7.45824156E+22 1.40129846E-45 -3.76901029E+22 4.56206728E-41 -9.23928235E+22 4.56206728E-41 -9.23890045E+22 4.56206728E-41 0.00000000 0.00000000 1.40129846E-45 0.00000000 -9.23812943E+22 4.56206728E-41 NaN 0.00000000 -7.46709924E+22 1.40129846E-45 -3.77340400E+22 4.56206728E-41 -7.43476564E+22 4.56206728E-41 -3.43335805E-17 4.59163468E-41 -1.11285239E+33 3.09168480E-41 -3.43343217E-17 4.59163468E-41 0.00000000 0.00000000 0.00000000 0.00000000 -7.46709924E+22 4.56206728E-41 1.40129846E-45 0.00000000 0.00000000 0.00000000 -9.27450813E+33 3.09168480E-41 -3.76901029E+22 4.56206728E-41 -3.43333688E-17 4.59163468E-41 -7.48105274E+22 4.56206728E-41 1.40129846E-45 0.00000000 -1.11294152E+33 3.09168480E-41 4.34402524E-44 0.00000000 -5.65814243E+22 4.56206728E-41 4.20389539E-45 0.00000000 -5.60198794E+22 4.56206728E-41 -5.60198794E+22 4.56206728E-41 -7.48105274E+22 4.56206728E-41 1.40129846E-45 0.00000000 1.01397116E-38 0.00000000 8.40779079E-45 0.00000000 1.13000708E-41 9.18340949E-41 3.36311631E-44 0.00000000 -9.27497359E+33 3.09168480E-41 0.00000000 0.00000000 -7.48105274E+22 4.56206728E-41 2.00000000 0.00000000 0.00000000 0.00000000 -3.43332100E-17 4.59163468E-41 1.13000708E-41 9.18340949E-41 -7.43579697E+22 4.56206728E-41 -9.27497359E+33 3.09168480E-41 0.00000000 0.00000000 NaN NaN 1.75162308E-43 1.76563607E-43 1.77964905E-43 1.79366203E-43 -5.59999195E+22 4.56206728E-41 1.13000708E-41 9.18340949E-41 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 NaN 0.00000000 NaN 1.75162308E-43 1.76563607E-43 1.77964905E-43 1.79366203E-43 -5.59999195E+22 4.56206728E-41 -5.59999195E+22 4.56206728E-41 1.96181785E-44 0.00000000 1.96181785E-44 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 NaN NaN NaN NaN 5.60519386E-45 5.60519386E-45 5.60519386E-45 5.60519386E-45 -5.60090708E+22 4.56206728E-41 -5.60090708E+22 4.56206728E-41 -5.65814243E+22 4.56206728E-41 6.46234854E-27 0.00000000 -5.65814243E+22 4.56206728E-41 6.46234854E-27 0.00000000 -3.86449200E+22 4.56206728E-41 2.35098870E-38 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 2.80259693E-45 0.00000000 1.96181785E-44 -0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 2.80259693E-45 0.00000000 1.96181785E-44 -0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 -9.38126010E-25 -6.45877724E-35 0.00000000 0.00000000 -9.38126010E-25 -6.45877724E-35 -5.65814243E+22 4.56206728E-41 -3.86429294E+22 4.56206728E-41 -5.65814243E+22 4.56206728E-41 2.35098870E-38 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 2.52435490E-29 4.56206728E-41 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 2.80259693E-45 0.00000000 1.96181785E-44 -0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 2.80259693E-45 0.00000000 1.96181785E-44 -0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
矩阵的插入行为与我的预期不同。所有插入的元素都在哪里?当我想将矩阵相乘时,它不起作用。如何以更好的方式插入?
您声明了20 x 20的矩阵。但是,您仅读取2x2元素。因此,所有其他元素都充满了垃圾。
您可以根据需要的大小分配矩阵。