只是为了好玩,你也可以使用do-loops作为大A矩阵
do i = 1, N A( i, : ) = [ A1( i,: ), A2( i,: ), A3( i,: ) ] A( i + N, : ) = [ A4( i,: ), A5( i,: ), A6( i,: ) ] A( i + N*2, : ) = [ A7( i,: ), A8( i,: ), A9( i,: ) ] enddo
它以行主方式填充A矩阵,因此小矩阵也以这种方式出现.如果真的非常必要,这也可以写成一行
A = transpose( reshape( & [ ( [ A1( i,: ), A2( i,: ), A3( i,: ) ], i=1,N ), & ( [ A4( i,: ), A5( i,: ), A6( i,: ) ], i=1,N ), & ( [ A7( i,: ), A8( i,: ), A9( i,: ) ], i=1,N ) ], [N*3, N*3] ))
原来是@francescalus答案中的第二个数组构造函数的转置(单行形式)
A = reshape( & [ ( [ A1( :,i ), A4( :,i ), A7( :,i ) ], i=1,N ), & ( [ A2( :,i ), A5( :,i ), A8( :,i ) ], i=1,N ), & ( [ A3( :,i ), A6( :,i ), A9( :,i ) ], i=1,N ) ], [N*3, N*3] )
为了更进一步,我们可以像在其他语言中一样定义hcat
和vcat
例程(请注意,显式接口是必需的):
function hcat( A, B, C ) result( X ) integer, dimension(:,:) :: A, B, C integer :: X( size(A,1), size(A,2)+size(B,2)+size(C,2) ) X = reshape( [ A, B, C ], shape( X ) ) endfunction function vcat( A, B, C ) result( X ) integer, dimension(:,:) :: A, B, C integer :: X( size(A,1)+size(B,1)+size(C,1), size(A,2) ) X = transpose( reshape( & [ transpose(A), transpose(B), transpose(C) ], & [ size(X,2), size(X,1) ] ) ) endfunction
那我们就可以写了
A = vcat( hcat( A1, A2, A3 ), hcat( A4, A5, A6 ), hcat( A7, A8, A9 ) )
这与问题中所需的形式有点类似:
A = [ A1 A2 A3 ; A4 A5 A6 ; A7 A8 A9 ]