How can I convert a matrix into matlab form?
matrix2Matlab[matrix0_] := Module[{matrix = matrix0}, "[" <> StringReplace[ ExportString[matrix, "CSV"], {"\n" -> ";", "," -> " "}] <> "]"]
matrix2Matlab[m = IdentityMatrix[3]]
(*
[1 0 0;0 1 0;0 0 1]
*)
Are there more simpler ways?
I use MATLink to deal with the string matrix.
ToMatlab
package... See my answer in the linked question for details on obtaining the package and using it. For your case, all you need to do isToMatlab@IdentityMatrix@3
. However, if you're using MATLink, you don't need to do any of this... could you perhaps clarify your usage? You can transfer to MATLAB usingMSet["mat", IdentityMatrix@3]
... $\endgroup$matrixList={IdentityMatrix[3],Partition[Range[9],3]}; MATLink
MEvaluate@"m = [ 1 0 0; 0 1 0; 0 0 1]; m(:,:,2) = [1 2 3;4 5 6;7 8 9;]";` $\endgroup$matrixList
here is really a 3D array, so that's what you get withMSet["s", matrixList]
. If you want to treat it as a list of two matrices, you can represent it as a cell which contains two 3x3 matrices and transfer it usingMSet["c", MCell[matrixList]]
. $\endgroup$MATLinkMEvaluate@"m = [ 1 0 0; 0 1 0; 0 0 1]; m(:,:,2) = [1 2 3;4 5 6;7 8 9;]";
$\endgroup$MSet[..., matrixList]
gives you, isn't it? $\endgroup$