
- Fortran 教程
- Fortran - 主頁
- Fortran - 概述
- Fortran - 環境設定
- Fortran - 基本語法
- Fortran - 資料型別
- Fortran - 變數
- Fortran - 常量
- Fortran - 運算子
- Fortran - 決策
- Fortran - 迴圈
- Fortran - 數值
- Fortran - 字元
- Fortran - 字串
- Fortran - 陣列
- Fortran - 動態陣列
- Fortran - 派生資料型別
- Fortran - 指標
- Fortran - 基本輸入/輸出
- Fortran - 檔案輸入/輸出
- Fortran - 過程
- Fortran - 模組
- Fortran - 內建函式
- Fortran - 數值精度
- Fortran - 程式庫
- Fortran - 程式設計風格
- Fortran - 除錯程式
- Fortran 資源
- Fortran - 快速指南
- Fortran - 有用資源
- Fortran - 討論
Fortran - 重塑函式
下表描述了 reshape 函式
函式 | 描述 |
---|---|
reshape(源,形狀,填充,順序) | 它利用給定陣列源中的元素構建一個指定形狀形狀的陣列。如果未包含填充,則源的大小必須至少為乘積(形狀)。如果包含填充,它必須與源具有相同的型別。如果包含順序,則它必須是具有與形狀相同的形狀的整數陣列,並且其值必須是(1,2,3,...,n)的排列,其中 n 是形狀中元素的數量,它必須小於或等於 7。 |
示例
以下示例演示了該概念
program arrayReshape implicit none interface subroutine write_matrix(a) real, dimension(:,:) :: a end subroutine write_matrix end interface real, dimension (1:9) :: b = (/ 21, 22, 23, 24, 25, 26, 27, 28, 29 /) real, dimension (1:3, 1:3) :: c, d, e real, dimension (1:4, 1:4) :: f, g, h integer, dimension (1:2) :: order1 = (/ 1, 2 /) integer, dimension (1:2) :: order2 = (/ 2, 1 /) real, dimension (1:16) :: pad1 = (/ -1, -2, -3, -4, -5, -6, -7, -8, & & -9, -10, -11, -12, -13, -14, -15, -16 /) c = reshape( b, (/ 3, 3 /) ) call write_matrix(c) d = reshape( b, (/ 3, 3 /), order = order1) call write_matrix(d) e = reshape( b, (/ 3, 3 /), order = order2) call write_matrix(e) f = reshape( b, (/ 4, 4 /), pad = pad1) call write_matrix(f) g = reshape( b, (/ 4, 4 /), pad = pad1, order = order1) call write_matrix(g) h = reshape( b, (/ 4, 4 /), pad = pad1, order = order2) call write_matrix(h) end program arrayReshape subroutine write_matrix(a) real, dimension(:,:) :: a write(*,*) do i = lbound(a,1), ubound(a,1) write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2)) end do end subroutine write_matrix
編譯並執行以上程式碼後,它將生成以下結果
21.0000000 24.0000000 27.0000000 22.0000000 25.0000000 28.0000000 23.0000000 26.0000000 29.0000000 21.0000000 24.0000000 27.0000000 22.0000000 25.0000000 28.0000000 23.0000000 26.0000000 29.0000000 21.0000000 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000 21.0000000 25.0000000 29.0000000 -4.00000000 22.0000000 26.0000000 -1.00000000 -5.00000000 23.0000000 27.0000000 -2.00000000 -6.00000000 24.0000000 28.0000000 -3.00000000 -7.00000000 21.0000000 25.0000000 29.0000000 -4.00000000 22.0000000 26.0000000 -1.00000000 -5.00000000 23.0000000 27.0000000 -2.00000000 -6.00000000 24.0000000 28.0000000 -3.00000000 -7.00000000 21.0000000 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000 -1.00000000 -2.00000000 -3.00000000 -4.00000000 -5.00000000 -6.00000000 -7.00000000
fortran_arrays.htm
廣告