Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 正确的struc数组声明_Fortran_Gfortran - Fatal编程技术网

Fortran 正确的struc数组声明

Fortran 正确的struc数组声明,fortran,gfortran,Fortran,Gfortran,我正在尝试将matlab/gnu倍频程代码翻译成gfortran语言代码 我已经完成了struc声明,以形成一个数组,因为它是用matlab编写的。但是,由于我不理解的数组问题,编译器将不会处理SIZE命令。下面是我要翻译的八度代码 function test1 clc; close all; start= 5; function out = function1(start) myarray = struct(... 'list1', {'A', 'Cc', 'B', 'E', 'F',

我正在尝试将matlab/gnu倍频程代码翻译成gfortran语言代码

我已经完成了struc声明,以形成一个数组,因为它是用matlab编写的。但是,由于我不理解的数组问题,编译器将不会处理SIZE命令。下面是我要翻译的八度代码

function test1
clc;
close all;
start= 5;
function out = function1(start)
myarray = struct(...
    'list1', {'A', 'Cc', 'B', 'E', 'F', 'G', 'H', 'I'}, ...
    'list2', {1, 2,  3,  4, 5, 6, 7, 8},...
    'list3', {3, 3, 3, 3, 3, 2, 2, 2});
function list1 = function2(list2)
    done = false;
    myarray    
    numel(myarray)
    ra = randi([1 numel(myarray)], 1, 1)
    myarray(ra).list2;

    list1 = myarray(ra);
    if list1.list1 == 'E'
        list1.list1 = round(randn(1,1) * 5);
    end
   end
  x = {}; 
  y = [];
  list2 = 0;    
    list1 = function2(list2);
    list2 = list2 + list1.list3;
  out = struct('x', {x}, 'y', {y});
 end
function1(5)
end
倍频程码的输出是

test1

myarray =

1x8 struct array containing the fields:

list1
list2
list3

ans =  8
ra =  1
ans =

scalar structure containing the fields:

x = {}(0x0)
y = [](0x0) 
我的gfortran代码是

function function1(start) result(out1)

 type myarray
 character, dimension(8)::list1=(/'A','C','B','E','F','G','H','I'/); 
 integer, dimension(8)::list2 =(/1,2,3,4,5,6,7,8/); 
 integer, dimension(8)::list3 =(/3,3,3,3,3,2,2/);
  end type 

  integer :: start;
  integer :: out1;
  integer :: sd;

 contains

  function function2(list2) result(list1)

   integer, intent(in) :: list2; ! input
   integer :: list1;             ! output  
   logical :: done;  
   integer, dimension(1,1) :: ra;
   real    :: rnd1;

    done = .FALSE.


    call random_number(rnd1);
    ra = nint(rnd1*5);
    sd= size(myarray);
   ! ra = nint(rnd1*size(myarray));
       print*, "random number is ", ra;
     myarray(ra)

 end function function2

 end function function1

program test1
use iso_fortran_env
implicit none

integer                 :: start, xout;
real                    :: ra;
integer                 :: function1;

start =5;
xout=function1(5);
end program test1
我从build命令中得到的错误消息是:

gfortran  -Ofast -Wall -o "test1" "test1.f90"  
test1.f90:32:16:
      myarray(ra).list2;
            1
Error: Derived type ‘myarray’ cannot be used as a variable at (1)
test1.f90:29:17:
     sd= size(myarray);
             1
Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array
Compilation failed.

我相信问题在于我对数组结构的声明。我错过了一些东西。有什么建议吗?请注意,在清单1值中的gfortran代码中,我必须将字符“Cc”更改为“C”才能工作。

与Matlab中的情况不同,在Matlab中,
struct
使用给定的值创建对象,Fortran中的
type
语句不会

相反,
键入myarray
定义对象的外观。没有创建任何对象,我们需要执行以下操作

type myarray
   ...   ! The definition of components
end type myarray
type(myarray) obj ! Declare an object of the defined type.
在此之后,
obj
是您感兴趣的对象

然而,还有更多需要注意的。与

type myarray
  character, dimension(8)::list1=(/'A','C','B','E','F','G','H','I'/); 
  integer, dimension(8)::list2 =(/1,2,3,4,5,6,7,8/); 
  integer, dimension(8)::list3 =(/3,3,3,3,3,2,2/);
end type 
您没有(再次)创建对象。当您创建一个对象(使用
type(myarray)obj
)时,该对象很可能以指定的值开始。但是,这与您在Matlab
struct
语句中预期的情况不太一样。您可以阅读有关默认初始化和构造函数的详细信息

说到
size(myarray)
,一个单独声明为
type(myarray)obj
的对象是一个标量对象。这就是“结构数组”和“数组结构”之间的区别。“结构”
myarray
包含三个数组,每个数组的长度为8。要使用结构数组,请执行以下操作:

type myarray
  character list1
  integer list2, list3
end type myarray
type(myarray), dimension(8) :: array
但是,您必须构造数组

array = [myarray('A',1,3), myarray('C',2,3), ...]

其他问题的答案涉及构造此类结构阵列的细节。

命令“大小”与“更改大小”无关。它与确定阵列大小有关。matlab numel的意思是确定阵列的尺寸,即8。我认为这相当于“大小”在gfortran中。是的,我没有正确地编写该部分。我想编写(并将进行编辑)的是Fortran代码中的派生类型对象是标量。您的数组构造示例解决了我的问题。尽管“Cc”无法使用,我将看看我是否能解决这个问题或侥幸逃脱。那么list1.list1的fortran等价物是什么(八度)?如果你的意思是
myarray.list1
,那么
array%list1
。你应该在使用fortran派生类型的任何(好的)描述中找到这些细节。