-2
$\begingroup$

I want to convert the following 16x16 matrix containing the variable n and some integers into matlab programming language:

m={{0, 0, 2 Sqrt[4 + n], 0, Sqrt[4 + n], 0, 0, 0, Sqrt[4 + n], 0, 0, 0, 
  0, 0, 0, 0}, {0, 0, 0, 2 Sqrt[3 + n], 0, Sqrt[3 + n], 0, 0, 0, Sqrt[
  3 + n], 0, 0, 0, 0, 0, 0}, {2 Sqrt[4 + n], 0, 0, 0, 0, 0, Sqrt[
  3 + n], 0, 0, 0, Sqrt[3 + n], 0, 0, 0, 0, 0}, {0, 2 Sqrt[3 + n], 0, 
  0, 0, 0, 0, Sqrt[2 + n], 0, 0, 0, Sqrt[2 + n], 0, 0, 0, 0}, {Sqrt[
  4 + n], 0, 0, 0, 0, 0, 2 Sqrt[3 + n], 0, 0, 0, 0, 0, Sqrt[3 + n], 0,
   0, 0}, {0, Sqrt[3 + n], 0, 0, 0, 0, 0, 2 Sqrt[2 + n], 0, 0, 0, 0, 
  0, Sqrt[2 + n], 0, 0}, {0, 0, Sqrt[3 + n], 0, 2 Sqrt[3 + n], 0, 0, 
  0, 0, 0, 0, 0, 0, 0, Sqrt[2 + n], 0}, {0, 0, 0, Sqrt[2 + n], 0, 
  2 Sqrt[2 + n], 0, 0, 0, 0, 0, 0, 0, 0, 0, Sqrt[1 + n]}, {Sqrt[
  4 + n], 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 Sqrt[3 + n], 0, Sqrt[3 + n], 0,
   0, 0}, {0, Sqrt[3 + n], 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 Sqrt[2 + n], 
  0, Sqrt[2 + n], 0, 0}, {0, 0, Sqrt[3 + n], 0, 0, 0, 0, 0, 
  2 Sqrt[3 + n], 0, 0, 0, 0, 0, Sqrt[2 + n], 0}, {0, 0, 0, Sqrt[
  2 + n], 0, 0, 0, 0, 0, 2 Sqrt[2 + n], 0, 0, 0, 0, 0, Sqrt[
  1 + n]}, {0, 0, 0, 0, Sqrt[3 + n], 0, 0, 0, Sqrt[3 + n], 0, 0, 0, 0,
   0, 2 Sqrt[2 + n], 0}, {0, 0, 0, 0, 0, Sqrt[2 + n], 0, 0, 0, Sqrt[
  2 + n], 0, 0, 0, 0, 0, 2 Sqrt[1 + n]}, {0, 0, 0, 0, 0, 0, Sqrt[
  2 + n], 0, 0, 0, Sqrt[2 + n], 0, 2 Sqrt[2 + n], 0, 0, 0}, {0, 0, 0, 
  0, 0, 0, 0, Sqrt[1 + n], 0, 0, 0, Sqrt[1 + n], 0, 2 Sqrt[1 + n], 0, 
  0}}

What should I do? Thanks and Regards

$\endgroup$
3
  • 1
    $\begingroup$ You should read a bit more about MATLAB; plus this site is about Mathematica so the question is off-topic $\endgroup$
    – Sektor
    Commented Sep 29, 2015 at 18:23
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because it is about MatLab rather than Mathematica. $\endgroup$
    – Bob Hanlon
    Commented Sep 29, 2015 at 19:15
  • 1
    $\begingroup$ I regretted my close vote when I noticed it had an answer and I started to think about the question as a question about how to export data from Mathematica in a certain type of format, so I voted to reopen it. $\endgroup$
    – C. E.
    Commented Sep 29, 2015 at 19:34

2 Answers 2

9
$\begingroup$

I believe you need to specify n and convert everything to reals:

 "[" <> StringJoin[
       Riffle[StringSplit[ExportString[N@m, "Table"], "\n"] , ";"]] <> "]"

( with n=3 )

[0. 0. 5.291502622129181 0. 2.6457513110645907 0. 0. 0.
2.6457513110645907 0. 0. 0. 0. 0. 0. 0. ; 0. 0. 0. 4.898979485566356 0. 2.449489742783178 0. 0. 0. 2.449489742783178 .... ]

( you might need Transpose@m , I'm not sure how matlab orders multi dimensional matrices )

$\endgroup$
3
$\begingroup$

Multidimensional matrices can be converted to Matlab's input format using the ToMatlab package:

<< ToMatlab`

m = {{0, 0, 2 Sqrt[4 + n], 0, Sqrt[4 + n], 0, 0, 0, Sqrt[4 + n], 0, 0,  0, 0, 0, 0, 0},
   {0, 0, 0, 2 Sqrt[3 + n], 0, Sqrt[3 + n], 0, 0, 0, Sqrt[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0, 0},
   {0, 0, 0, 0, 0, 0, 0, Sqrt[1 + n], 0, 0, 0, Sqrt[1 + n], 0, 
    2 Sqrt[1 + n], 0, 0}};

ToMatlab[m /. n -> 1, "a"]
(* copy/paste-able string output *)

ToMatlab[m, "m"]
(* copy/paste-able string output *)

Copying the output as "plain text" to the Matlab editor:

n=1;

a=[0,0,2.*5.^(1/2),0,5.^(1/2),0,0,0,5.^(1/2),0,0,0,0,0,0,0;0,0,0,4, ...
  0,2,0,0,0,2,0,0,0,0,0,0;2.*5.^(1/2),0,0,0,0,0,2,0,0,0,2,0,0,0,0,0; ...
  0,4,0,0,0,0,0,3.^(1/2),0,0,0,3.^(1/2),0,0,0,0;5.^(1/2),0,0,0,0,0, ...
  4,0,0,0,0,0,2,0,0,0;0,2,0,0,0,0,0,2.*3.^(1/2),0,0,0,0,0,3.^(1/2), ...
  0,0;0,0,2,0,4,0,0,0,0,0,0,0,0,0,3.^(1/2),0;0,0,0,3.^(1/2),0,2.* ...
  3.^(1/2),0,0,0,0,0,0,0,0,0,2.^(1/2);5.^(1/2),0,0,0,0,0,0,0,0,0,4, ...
  0,2,0,0,0;0,2,0,0,0,0,0,0,0,0,0,2.*3.^(1/2),0,3.^(1/2),0,0;0,0,2, ...
  0,0,0,0,0,4,0,0,0,0,0,3.^(1/2),0;0,0,0,3.^(1/2),0,0,0,0,0,2.*3.^( ...
  1/2),0,0,0,0,0,2.^(1/2);0,0,0,0,2,0,0,0,2,0,0,0,0,0,2.*3.^(1/2),0; ...
  0,0,0,0,0,3.^(1/2),0,0,0,3.^(1/2),0,0,0,0,0,2.*2.^(1/2);0,0,0,0,0, ...
  0,3.^(1/2),0,0,0,3.^(1/2),0,2.*3.^(1/2),0,0,0;0,0,0,0,0,0,0,2.^( ...
  1/2),0,0,0,2.^(1/2),0,2.*2.^(1/2),0,0];

m=[0,0,2.*(4+n).^(1/2),0,(4+n).^(1/2),0,0,0,(4+n).^(1/2),0,0,0,0,0, ...
  0,0;0,0,0,2.*(3+n).^(1/2),0,(3+n).^(1/2),0,0,0,(3+n).^(1/2),0,0,0, ...
  0,0,0;2.*(4+n).^(1/2),0,0,0,0,0,(3+n).^(1/2),0,0,0,(3+n).^(1/2),0, ...
  0,0,0,0;0,2.*(3+n).^(1/2),0,0,0,0,0,(2+n).^(1/2),0,0,0,(2+n).^( ...
  1/2),0,0,0,0;(4+n).^(1/2),0,0,0,0,0,2.*(3+n).^(1/2),0,0,0,0,0,(3+ ...
  n).^(1/2),0,0,0;0,(3+n).^(1/2),0,0,0,0,0,2.*(2+n).^(1/2),0,0,0,0, ...
  0,(2+n).^(1/2),0,0;0,0,(3+n).^(1/2),0,2.*(3+n).^(1/2),0,0,0,0,0,0, ...
  0,0,0,(2+n).^(1/2),0;0,0,0,(2+n).^(1/2),0,2.*(2+n).^(1/2),0,0,0,0, ...
  0,0,0,0,0,(1+n).^(1/2);(4+n).^(1/2),0,0,0,0,0,0,0,0,0,2.*(3+n).^( ...
  1/2),0,(3+n).^(1/2),0,0,0;0,(3+n).^(1/2),0,0,0,0,0,0,0,0,0,2.*(2+ ...
  n).^(1/2),0,(2+n).^(1/2),0,0;0,0,(3+n).^(1/2),0,0,0,0,0,2.*(3+n) ...
  .^(1/2),0,0,0,0,0,(2+n).^(1/2),0;0,0,0,(2+n).^(1/2),0,0,0,0,0,2.*( ...
  2+n).^(1/2),0,0,0,0,0,(1+n).^(1/2);0,0,0,0,(3+n).^(1/2),0,0,0,(3+ ...
  n).^(1/2),0,0,0,0,0,2.*(2+n).^(1/2),0;0,0,0,0,0,(2+n).^(1/2),0,0, ...
  0,(2+n).^(1/2),0,0,0,0,0,2.*(1+n).^(1/2);0,0,0,0,0,0,(2+n).^(1/2), ...
  0,0,0,(2+n).^(1/2),0,2.*(2+n).^(1/2),0,0,0;0,0,0,0,0,0,0,(1+n).^( ...
  1/2),0,0,0,(1+n).^(1/2),0,2.*(1+n).^(1/2),0,0];

Now in the Matlab command window:

>>isequal(a,m)

ans =

     1
$\endgroup$

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