2016-12-16 9 views
0

以下の例のように、Fortranで寸法(6 * 4)のマトリックス(B)に3 * 8の寸法の2D行列(A)を再形成する必要があります。Fortran90での形状変更

Matrix A 

1 2 1 2 1 2 1 2 
5 6 5 6 5 6 5 6 
7 8 7 8 7 8 7 8 

Matrix B      
    1 1 1 1 
    2 2 2 2 
    5 5 5 5 
    6 6 6 6 
    7 7 7 7 
    8 8 8 8 

私はループで次のように試みましたが、大きなマトリックスでは非常に遅いようです。このような再形成が可能である場合、私は

 counter=1 
     do i=1,size(A,2),2 
      seq1(counter)=i 
      counter=1+counter 
     end do 

     counter=1 
     do i=2,size(A,2),2 
      seq2(counter)=i 
      counter=1+counter 
     end do 

     counter=1 
     do i=1,size(A,1)*2,2 
      S_1(counter)=i 
      counter=1+counter 
     end do 

     counter=1 
     do i=2,size(A,1)*2,2 
      S_2(counter)=i 
      counter=1+counter 
     end do 

     Do i=1,size(A,1) 
      B(S_1(i),:)=A(i,seq1) 
      B(S_2(i),:)=A(i,seq2) 
     END DO 

答えて

0

次のコードは、適切な整形を行うのFortran 90にRESHAPE機能と考えていたが、あなたはパフォーマンスを測定するためにプロファイラを通して、あなたのプログラムを実行する必要があります。最適化フラグはどれくらい積極的ですか?

B(1:2,:) = reshape(source=A(1,:), shape=[2,4]) 
    B(3:4,:) = reshape(source=A(2,:), shape=[2,4]) 
    B(5:6,:) = reshape(source=A(3,:), shape=[2,4]) 

と変換を逆にする、

A(1,:) = reshape(source=B(1:2,:), shape=[8]) 
    A(2,:) = reshape(source=B(3:4,:), shape=[8]) 
    A(3,:) = reshape(source=B(5:6,:), shape=[8]) 
関連する問題