正确的 struc 数组声明
Proper struc array declaration
我正在尝试将 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
我从构建命令中得到的错误消息是:
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.
我认为问题出在我对数组结构的声明上。我在那里遗漏了一些东西。有什么建议么?请注意,在 list1 值的 gfortran 代码中,我必须将字符 'Cc' 更改为 'C' 才能使其工作。
与 Matlab 的情况不同,struct
使用给定的值创建一个对象,Fortran 中的 type
语句不会。
相反,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
您不是(再次)创建对象。当您 do 创建一个对象时(使用 type(myarray) obj
,该对象可能以指定的值开始。这与您对 Matlab struct
声明,不过。您可以阅读有关 默认初始化 和 构造函数 的详细信息。
来到 size(myarray)
,单独声明为 type(myarray) obj
的对象是 标量 对象。这就是"arrays of structures"和"structures of arrays"的区别。 "structure" 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/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
我从构建命令中得到的错误消息是:
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.
我认为问题出在我对数组结构的声明上。我在那里遗漏了一些东西。有什么建议么?请注意,在 list1 值的 gfortran 代码中,我必须将字符 'Cc' 更改为 'C' 才能使其工作。
与 Matlab 的情况不同,struct
使用给定的值创建一个对象,Fortran 中的 type
语句不会。
相反,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
您不是(再次)创建对象。当您 do 创建一个对象时(使用 type(myarray) obj
,该对象可能以指定的值开始。这与您对 Matlab struct
声明,不过。您可以阅读有关 默认初始化 和 构造函数 的详细信息。
来到 size(myarray)
,单独声明为 type(myarray) obj
的对象是 标量 对象。这就是"arrays of structures"和"structures of arrays"的区别。 "structure" 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), ...]
其他问题的答案涉及构建此类结构数组的细节。