0
$\begingroup$

How to show output of a matrix in m(1,1)=a11 m(1,2)=a12 ...in this way......i have 50 *50 size matrix

so i want to do it efficiently.

output to be shown like

m(1,1)=a11 (in fortranForm) m(1,2)=a12 (In fortranform) .... ....continue in this way, one element per one line.

i have specific reason of doing this. i will copy paste my data into Fortran code to use it this data in less time.

please help me with this

$\endgroup$
  • $\begingroup$ You mean like this !Mathematica graphics Array[a, {5, 5}]? Question not clear. What is the context? ps. I just saw your other question here mathematica.stackexchange.com/questions/65148/… is this different? $\endgroup$ – Nasser Nov 7 '14 at 19:39
  • $\begingroup$ Hii.....No other question is also same. i just want my matrix output to be shown like this matrix(1,1)=a11 element in fortran form matrix(1,2)=a12 element in fortranform....continue in this way $\endgroup$ – Amandeep Nov 7 '14 at 20:00
2
$\begingroup$

There is no subtlety to this, but it works:

Table[i j, {i, 5}, {j, 5}]
MapIndexed[Print["a(", #2[[1]], "," , #2[[2]], ") = ", #1] &, %, {2}];
$\endgroup$
  • $\begingroup$ What is %102? $\endgroup$ – Nasser Nov 7 '14 at 20:51
  • $\begingroup$ @Nasser leftovers my own evaluation. Removed. :P $\endgroup$ – rcollyer Nov 7 '14 at 20:54
  • $\begingroup$ Ok, now it makes sense :), I thought you were using some new symbol/command in V10 but could not find it. $\endgroup$ – Nasser Nov 7 '14 at 21:01
2
$\begingroup$

building on @rcollyer's answer: The main improvement here is we print the list of elements in a way that the whole list can be copied at once. (also handle arbitrary dimensions automatically )

 SetAttributes[fortranprint, HoldFirst]
 fortranprint[array_Symbol] := 
      fortranprint[array, SymbolName[Unevaluated[array]]];
 fortranprint[array_, name_] :=
      Print@StringJoin@Riffle[ Flatten@MapIndexed[ StringJoin@
          {"        ", (* leading spaces for fixed format fortran *)
           name,
          "(", Riffle[ (ToString /@ #2) , "," ], ")=",
           ToString[FortranForm[#]]} & , array, {-1}] , "\n"];

 b = {{1.432, 2. 10^30}, {0, 1}};
 fortranprint[b];
   b(1,1)=1.432
   b(1,2)=2.e30
   b(2,1)=0
   b(2,2)=1
 fortranprint[{{{1, 2}, {3, 4}}, {{5, 6}}}, "g"];
    g(1,1,1)=1
    g(1,1,2)=2
    g(1,2,1)=3
    g(1,2,2)=4
    g(2,1,1)=5
    g(2,1,2)=6
$\endgroup$
  • $\begingroup$ Yeah, mine was quick and dirty. I should have done this. +1 $\endgroup$ – rcollyer Nov 7 '14 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.