本文介绍了如何构建一个包含9个较小矩阵的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有九个矩阵,其维数为(N乘N) A1(i,j),A2(i,j),A3(i,j),A4(i, j),A5(i,j),A6(i,j),A7(i,j),A8(i,j),A9(i,j) $ b 然后我想构造一个更大的矩阵(3N乘3N),包括这9个矩阵: A = [A1 A2 A3 A4 A5 A6 A7 A8 A9] do i = 1,FN do j = 1 ,A(i,j)= A 1(i,j),A 2(i,j),A 3(i,j) (i,j); A7(i,j),A8(i,j),A9(i,j)] end do end do 解决方案为了好玩,您还可以通过使用do循环来制作大型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 其中fi以行为主方式排列A矩阵,所以小矩阵也以这种方式出现。如果确实需要的话,这也可以写成一行: A =转置(重塑(& ([A1(i ,:),A2(i ,:),A3(i ,:)],i = 1,N),& A8(i ,:),A9(i ,:)],A6(i ,:)],i = 1,N),& i = 1,N)],[N * 3,N * 3])) A = reshape(在一行中) & [([A1(:,i),A4(:,i),A7(:,i)],i = 1,N) (:,i),A5(:,i),A8(:i)],i = 1,N), ,i = 1,N)],[N * 3,N * 3]) 为了更进一步,我们可以定义 hcat 和 vcat 例程与其他语言一样(注意这里需要显式接口): funct (A,B,C)result(X) integer,dimension(:, :) :) A,B,C integer :: X(size(A,1),size( A,2)+尺寸(B,2)+尺寸(C,2)) X =重塑([A,B,C],形状(X)) endfunction 函数vcat(A,B,C)结果(X)整数,维(:, :) :: A,B,C integer :: X(size( A,1)+大小(B,1)+大小(C,1),大小(A,2)) X =转置(重塑(& [转置(A),转置(B),转置(C)],& [size(X,2),size(X,1)])) endfunction $ p $ $ $ code $ A = vcat(hcat(A1,A2,A3),hcat(A4, A5,A6),hcat(A7,A8,A9)) 在问题中需要的形式: A = [A1 A2 A3; A4 A5 A6; A7 A8 A9] I have nine matrices whose dimension as (N by N)A1(i,j),A2(i,j),A3(i,j),A4(i,j),A5(i,j),A6(i,j),A7(i,j),A8(i,j),A9(i,j)Then I want to construct a larger matrix (3N by 3N) including these nine matrices as:A = [A1 A2 A3 A4 A5 A6 A7 A8 A9]In fortran, can I use the command line asdo i=1,FN do j=1,FML A(i,j) = [A1(i,j),A2(i,j),A3(i,j);A4(i,j),A5(i,j),A6(i,j);A7(i,j),A8(i,j),A9(i,j)] end doend do 解决方案 Just for fun, you can also make the large A matrix by using do-loops asdo 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,: ) ]enddowhich fills the A matrix in row-major way and so the small matrices also appear in that way. If really really necessary, this could also be written as one-liner asA = 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] ))which turns out to be the transpose of the second array constructor in the @francescalus answer (in one-liner form)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] )To go one-step further, we may define hcat and vcat routines as in other languages (note here that explicit interface is necessary):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 ) )endfunctionfunction 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) ] ) )endfunctionthen we can writeA = vcat( hcat( A1, A2, A3 ), hcat( A4, A5, A6 ), hcat( A7, A8, A9 ) )which is somewhat more similar to the desired form in the question:A = [ A1 A2 A3 ; A4 A5 A6 ; A7 A8 A9 ] 这篇关于如何构建一个包含9个较小矩阵的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 16:15