I have this array:
我有这个数组:
const / var
_Data : array [0..4] of array [0..3] of Double =
((0,0,0,0),
(0,0,1,1),
(1,0,1,0),
(1,1,0,0),
(1,1,1,1));
I wanna pass it as param value for this procedure:
我想将它作为此过程的参数值传递:
procedure NN.NetTraining(Data: TDoubleMatrix);
Where:
TDoubleArray = array of Double;
TDoubleMatrix = array of TDoubleArray;
Is There some manner to cast or convert this static array into dynamic array in Delphi (2009) ?
在Delphi(2009)中是否有某种方式将此静态数组转换为动态数组?
Thanks in advance.
提前致谢。
4 个解决方案
#1
3
While this does not do exactly what you want (for reasons given in Gamecat's answer), it may be a viable work-around for you to initialise your dynamic data array:
虽然这并不完全符合您的要求(由于Gamecat的答案中给出的原因),但初始化动态数据阵列可能是一种可行的解决方法:
var Data:TDoubleMatrix;
begin
Data:=TDoubleMatrix.create(TDoubleArray.create(0,0,0,0),
TDoubleArray.create(0,0,1,1),
TDoubleArray.create(1,0,1,0),
TDoubleArray.create(1,1,0,0),
TDoubleArray.create(1,1,1,1));
end;
#2
1
Dynamic arrays differ from normal arrays.
动态数组与普通数组不同。
Dynamic arrays are pointers, while normal arrays are blocks of memory. With one dimensional arrays, you can use the address of the array. But with multi dimensional arrays this trick won't work.
动态数组是指针,而普通数组是内存块。使用一维数组,您可以使用数组的地址。但是对于多维数组,这个技巧将不起作用。
In your case, I would use a file to initialize the array. So you can use dynamic arrays 100% of the time. Else you have to write your own conversion which kind of defeats the purpose of dymanic arrays.
在你的情况下,我会使用一个文件来初始化数组。因此,您可以100%使用动态数组。否则你必须编写自己的转换,这会破坏dymanic数组的目的。
#3
0
You can't cast but you can copy! Something like...
你不能投,但你可以复制!就像是...
procedure CopyDMatrix(var Source; LengthX, LengthY: integer; var Dest: TDoubleMatrix);
const
SizeOfDouble = SizeOf(Double);
var
n : integer;
ptr: pointer;
begin
SetLength(Dest, LengthX);
Ptr := @Source;
for n := 0 to LengthX - 1 do begin
SetLength(Dest[n], LengthY);
Move(ptr^, Dest[n][0] , SizeOfDouble * LengthY);
inc(cardinal(ptr), SizeOfDouble * LengthY);
end;
end;
//...
CopyDMatrix(Data, Length(Data), Length(Data[0]), DoubleMatrix);
#1
3
While this does not do exactly what you want (for reasons given in Gamecat's answer), it may be a viable work-around for you to initialise your dynamic data array:
虽然这并不完全符合您的要求(由于Gamecat的答案中给出的原因),但初始化动态数据阵列可能是一种可行的解决方法:
var Data:TDoubleMatrix;
begin
Data:=TDoubleMatrix.create(TDoubleArray.create(0,0,0,0),
TDoubleArray.create(0,0,1,1),
TDoubleArray.create(1,0,1,0),
TDoubleArray.create(1,1,0,0),
TDoubleArray.create(1,1,1,1));
end;
#2
1
Dynamic arrays differ from normal arrays.
动态数组与普通数组不同。
Dynamic arrays are pointers, while normal arrays are blocks of memory. With one dimensional arrays, you can use the address of the array. But with multi dimensional arrays this trick won't work.
动态数组是指针,而普通数组是内存块。使用一维数组,您可以使用数组的地址。但是对于多维数组,这个技巧将不起作用。
In your case, I would use a file to initialize the array. So you can use dynamic arrays 100% of the time. Else you have to write your own conversion which kind of defeats the purpose of dymanic arrays.
在你的情况下,我会使用一个文件来初始化数组。因此,您可以100%使用动态数组。否则你必须编写自己的转换,这会破坏dymanic数组的目的。
#3
0
You can't cast but you can copy! Something like...
你不能投,但你可以复制!就像是...
procedure CopyDMatrix(var Source; LengthX, LengthY: integer; var Dest: TDoubleMatrix);
const
SizeOfDouble = SizeOf(Double);
var
n : integer;
ptr: pointer;
begin
SetLength(Dest, LengthX);
Ptr := @Source;
for n := 0 to LengthX - 1 do begin
SetLength(Dest[n], LengthY);
Move(ptr^, Dest[n][0] , SizeOfDouble * LengthY);
inc(cardinal(ptr), SizeOfDouble * LengthY);
end;
end;
//...
CopyDMatrix(Data, Length(Data), Length(Data[0]), DoubleMatrix);