对一部分做数值计算的初级编程人员而言,往往分不清数组元素的优先排列顺序,本文以Fortran、Matlab、Python三种编程语言进行简要说明。
1. Fortran:列优先
integer :: a(2,2)
a = reshape([1,2,3,4])
do i = 1, 2
write( *,'(*(g0,3x))' ) a(i,:)
end do
end program
数组a的输出结果:
1 3
2 4
2. Matlab:列优先
a = [1,2,3,4];
b = reshape(a,[2,2]);
数组b的输出结果:
1 3
2 4
3. Python:行优先
import numpy as np
a = np.array([1,2,3,4])
b = np.reshape(a,[2,2])
数组b的输出结果:
1 2
3 4