Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

initialize a dynamic array

Posted on 2009-05-08
6
Medium Priority
?
2,090 Views
Last Modified: 2012-05-06
I need many 2D Matrices in different dimensions.
I#m working with a dynamic array  2DMatrix_REAL;

How to initialize the dyn. array in an elegant way.

Current solution

      setlength(Amatrix, x,y)

      Amatrix[0,0] := .....     Amatrix[ ,] :=    .......

      takes millions of lines of code.!
      Can I copy the Static array Identity  to a Dynamic Array ?
Identity : array [1..5, 1..5] of Real  =
                         ((  0,    0,    0 , 0   , 0   ) ,
                          (  0 ,   0,    0 , 0   , 0   ) ,
                          (  0,    0,    1 , 0   , 0   ) ,
                          (  0 ,   0,    0 , 0   , 0   ) ,
                          (  0 ,   0,    0 , 0   , 0   ) );
 
 
 
type   2DMatrix_REAL =  array of array of Real;

Open in new window

0
Comment
Question by:BdLm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 24343709
you can iterate through it

var i, j: integer;
begin
 setlength(Amatrix, x,y)
 for i := 0 to x - 1 do
  for j := 0 to y - 1 do
   Amatrix[x, y] := 0;
end;

Open in new window

0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24344147
Couldn't you create a buffer that mapped over the array (using the Absolute directive), then initialise that in code?

From Delphi Help:-

Absolute Addresses
You can create a new variable that resides at the same address as another variable. To do so, put the directive absolute after the type name in the declaration of the new variable, followed by the name of an existing (previously declared) variable. For example,

var
   Str: string[32];
   StrLen: Byte absolute Str;
specifies that the variable StrLen should start at the same address as Str. Since the first byte of a short string contains the string's length, the value of StrLen is the length of Str.

You cannot initialize a variable in an absolute declaration or combine absolute with any other directives.

0
 
LVL 19

Expert Comment

by:MerijnB
ID: 24345132
You have to be careful with that since a dynamic array is actually a pointer.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Author Comment

by:BdLm
ID: 24345417
any better solution than this one,

      TFilter3D = array [1..3,1..3] of Real;
      TFilter4D = array [1..4,1..4] of Real;
      TFilter5D = array [1..5,1..5] of Real;
      TFilter6D = array [1..6,1..6] of Real;
      TFilter7D = array [1..7,1..7] of Real;
 
 
const SampleFiler  : TFilter3D  =
                         ((  0 ,   0.2 , 0   ) ,
                          (  0.2,  0.2,  0.2 ) ,
                          (  0 ,   0.2 , 0   ));
 
 
 
 
 
 
 
//  copy static array to dyn. array  dim =6
procedure LoadDynArrayFromStaticArray ( aFilter :  TImageFilterReal;  Dim : Integer; aStaticArray : TFilter6D ); overload;
var  i,j   : Integer;
begin
      setlength(AFilter, dim,dim);
 
      for i := 0 to dim -1  do
        for j := 0 to dim-1 do
             aFilter[i,j] := aStaticArray[i+1,j+1];
 
end;
 
//  copy static array to dyn. array  dim =5
procedure LoadDynArrayFromStaticArray ( aFilter :  TImageFilterReal;  Dim : Integer; aStaticArray : TFilter5D ); overload;
var  i,j   : Integer;
begin
      setlength(AFilter, dim,dim);
 
      for i := 0 to dim -1  do
        for j := 0 to dim-1 do
             aFilter[i,j] := aStaticArray[i+1,j+1];
 
end;
 
//  copy static array to dyn. array  dim =4
procedure LoadDynArrayFromStaticArray ( aFilter :  TImageFilterReal;  Dim : Integer; aStaticArray : TFilter4D ); overload;
var  i,j   : Integer;
begin
      setlength(AFilter, dim,dim);
 
      for i := 0 to dim -1  do
        for j := 0 to dim-1 do
             aFilter[i,j] := aStaticArray[i+1,j+1];
 
end;
 
//  copy static array to dyn. array  dim =3
procedure LoadDynArrayFromStaticArray ( aFilter :  TImageFilterReal;  Dim : Integer; aStaticArray : TFilter3D ); overload;
var  i,j   : Integer;
begin
      setlength(AFilter, dim,dim);
 
      for i := 0 to dim -1  do
        for j := 0 to dim-1 do
             aFilter[i,j] := aStaticArray[i+1,j+1];
 
end;
 
 
 
procedure LoadImageFilterByIndex( aFilterNameIndex : integer;  var  aFilter : TImageFilterReal);
var  i ,j :  Integer;
begin
        case aFilterNameIndex  of
 
        0:   LoadDynArrayFromStaticArray ( aFilter, 3 , BasicBlur );
 
        1:    begin
 
              end;
        2:    begin
 
              end;
        3:    begin
 
 
              end;
 
        else
 
 
        end;
 
 
 
end;

Open in new window

0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 24351670
Easiest way is to simply copy the data using move function. see sample:
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
 
type
  T2dMatrix_REAL = array[1..5, 1..5] of real;
  T2DMatrix_Rec = record
    Description: string;
    Matrix: T2DMatrix_REAL;
  end;
  TMatrices = array of T2dMatrix_Rec;
 
const
 
  Identity : T2dMatrix_REAL  =
                         ((  1,    2,    3 , 4   , 5   ) ,
                          (  6 ,   7,    8 , 0   , 10  ) ,
                          (  11,   12,   13, 14  , 15  ) ,
                          (  0 ,   0,    0 , 0   , 0   ) ,
                          (  0 ,   0,    0 , 0   , 0   ) );
 
 
 
 
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    Matrices: TMatrices;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var c: char;
    i: integer;
begin
  SetLength(Matrices, 10);
  for c:= 'A' to 'J' do
  begin
    i:= Ord(c) - Ord('A');
    Matrices[i].Description:= c+c+c+c+c;
    move(identity[1,1], matrices[i].Matrix[1,1], SizeOf(T2dMatrix_REAL));
  end;
  if (Matrices[1].Matrix[1,1] = 0) then;
end;
 
end.

Open in new window

0
 
LVL 8

Author Closing Comment

by:BdLm
ID: 31579646
may be Delphi does not offer any more compact solution
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question