Message Boards Message Boards


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

Posted 5 months ago
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


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



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.



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:


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

Tx, Andras

Posted 4 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.


Dear Hans,

Very elegant, thank you very much!

best regards


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

Group Abstract Group Abstract