Message Boards Message Boards

GROUPS:

Specify a 3D array, where array elements contain other kind of variables?

Posted 3 months ago
391 Views
|
7 Replies
|
0 Total Likes
|

Dear Community,

In Fortran I can easily specify a 3D array, where array elements contain other kind of variables(real, integer, logical, vector, etc) This is very effivcient and conveniet. An example is below:

GBLOCK( 88 , 21 , 12 ).BO         = 1.22           (real)   ;
GBLOCK( 88 , 21 , 12 ).KRO       = 0.7             (real)   ;
GBLOCK( 88 , 21 , 12 ).ACTIVE  = .TRUE.      (logical)  ;
GBLOCK( 88 , 21 , 12 ).CT(2)      = 55.7          (vector, real) ;
GBLOCK( 88 , 21 , 12 ).CT(4)      = 19.2          (vector, real) ;
GBLOCK( 88 , 21 , 12 ).IBLOCK  = 43561      (integer) ;

My question is, how could I specify a similar structure in Mathematica? What would be the equivalent of the above array in Mathematica?

Tx for the kind help in advance, best regards Andras

7 Replies

There's no requirement that the members of a Mathematica list be of the same type

In[1]:= ar = {1, 0.3, "test", \[Pi], 1 + I, \[Infinity]}

Out[1]= {1, 0.3, "test", \[Pi], 1 + I, \[Infinity]}

Head[x] -> Symbol

Head["the"] -> String

Head[7] -> ...

as the last poster said, mathematica List[{a,b,c}] or Array[?], you do not need to worry about mixing types

furthermore, arrays in Mathematica are associative

Ar["the"]:=1
Ar["that"]:="there"
Ar["that"]:=Ar["the"]

all that works, and we expect if there are many strings for indicies we expect mathematica employs at least a binary search to cross reference the value

Tx for the contributions.

I have finally found an easy way to do it. One possible solution is in the attached notebook.

cheers, Andras

Attachments:

Andras,

I think you are overcomplicating things. As Frank pointed out, you can automatically put different types in an array.

You can just put whatever you want in the array and reference individual elements:

In[20]:= var = {{18, False, 12.2}, {23.111, True, 34.11}}

Out[20]= {{18, False, 12.2}, {23.111, True, 34.11}}

In[21]:= var[[2]]

Out[21]= {23.111, True, 34.11}

In[23]:= var[[2, 1]] = 24;

In[18]:= var[[2]]

Out[18]= {24, True, 34.11}

I do not understand why you need all that indirect referencing. Maybe I am missing something.

Regards,

Neil

I need this indirect referencing because then I do not have to keep in mind in which position an element in the list is. E.g. activeind = 3 , so I can referrence an active / inactive 3D grid property easily by gblock[[ j, i, k, activeind]] = True (or False) . (The Dollar sign was removed from activeind.) I'm actually transfering an older Fortran code of mine into the Wolfram Language, and this seemed to me the easiest way to do it. In Fortran I do the same this way:

GBLOCK(J,I,K).ACTIVE = .TRUE. (or .FALSE.)

If there is a more efficient way, I would be gratefuly to learn it.

Tx, Andras

Posted 2 months ago

As an alternative you could make an array of associations. And use the associations keys to access the elements. In the attachment I have made an adaption of your notebook.

Attachments:

Dear Hans,

Very elegant, thank you very much!

best regards

Andras

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract