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

Posted 4 years ago
4861 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
Sort By:
Posted 4 years ago
 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]} 
Anonymous User
Anonymous User
Posted 4 years ago
 Head[x] -> SymbolHead["the"] -> StringHead[7] -> ...as the last poster said, mathematica List[{a,b,c}] or Array[?], you do not need to worry about mixing typesfurthermore, 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
Posted 4 years ago
 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:
Posted 4 years ago
 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
Posted 4 years ago
 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 4 years 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:
Posted 4 years ago
 Dear Hans,Very elegant, thank you very much!best regardsAndras