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.
ToMatlabpackage... 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]... – rm -rf Oct 31 '13 at 02:25matrixList={IdentityMatrix[3],Partition[Range[9],3]}; MATLinkMEvaluate@"m = [ 1 0 0; 0 1 0; 0 0 1]; m(:,:,2) = [1 2 3;4 5 6;7 8 9;]";` – user92733 Oct 31 '13 at 03:55matrixListhere 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]]. – Szabolcs Oct 31 '13 at 04:06MATLinkMEvaluate@"m = [ 1 0 0; 0 1 0; 0 0 1]; m(:,:,2) = [1 2 3;4 5 6;7 8 9;]";– user92733 Oct 31 '13 at 04:16MSet[..., matrixList]gives you, isn't it? – Szabolcs Oct 31 '13 at 05:10Transpose@matrixList and Transpose/@matrixList– user92733 Oct 31 '13 at 05:51Transposeto achieve the proper permutation of the dimensions. Check the documentation for details please. In MATLAB the analogous function is calledpermute. Also note that while MATLAB printsm(:,:,1)andm(:,:,2), the matrices can be obtained bym(1,:,:)andm(2,:,:)without any transposition needed. – Szabolcs Oct 31 '13 at 15:39Transpose. – user92733 Nov 01 '13 at 11:54