fortran的reshape怎么用
时间: 2024-06-14 12:01:42 浏览: 23
Fortran中的reshape函数可以用来改变数组的形状。它的基本语法如下:
```
result_array = RESHAPE(source_array, shape)
```
其中,source_array是原始数组,shape是一个整数数组,用于描述结果数组的形状。reshape函数将原始数组按照新的形状进行重新排列,并返回一个新的结果数组。需要注意的是,新的形状必须与原始数组中元素的总数相同,否则会出现运行时错误。
以下是一个简单的示例代码,用于将一个一维数组转换为二维数组:
```
program reshape_example
implicit none
integer :: a(6) = [1, 2, 3, 4, 5, 6]
integer :: b(2, 3)
b = reshape(a, [2, 3])
write(*,*) 'Original array: ', a
write(*,*) 'Reshaped array: ', b
end program reshape_example
```
输出结果如下:
```
Original array: 1 2 3 4 5 6
Reshaped array: 1 2 3
4 5 6
```