Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 函数从数组创建字符串(Mathematica)_Arrays_String_Function_Wolfram Mathematica - Fatal编程技术网

Arrays 函数从数组创建字符串(Mathematica)

Arrays 函数从数组创建字符串(Mathematica),arrays,string,function,wolfram-mathematica,Arrays,String,Function,Wolfram Mathematica,我是Mathematica编程的新手,我需要一些帮助。我正在尝试编写一个函数,该函数获取任意数组的元素,并构建一个特殊格式的字符串,以便在Math LibreOffice中使用 我的代码如下: OOForm[MM_] := (strMM = "left ( matrix{"; For[i = 1, i < Dimensions[MM][[1]], i++] { (* not last row *) For[j = 1, j < Dimensions[M

我是Mathematica编程的新手,我需要一些帮助。我正在尝试编写一个函数,该函数获取任意数组的元素,并构建一个特殊格式的字符串,以便在Math LibreOffice中使用

我的代码如下:

OOForm[MM_] :=
   (strMM = "left ( matrix{";
   For[i = 1, i < Dimensions[MM][[1]], i++] {  (* not last row *)   
      For[j = 1, j < Dimensions[MM][[2]], j++] { (* not last element from the row *)    
         strMM = strMM <> ToString[MM[[i, j]], InputForm] <> "#";
      }; (* last element from the row *) 
      strMM = strMM <> ToString[MM[[i, Dimensions[MM][[2]]]], InputForm] <> "##"; 
   };
   For[j = 1, j < Dimensions[MM][[2]], j++] { (* last row without the last element *) 
      strMM = strMM <> ToString[MM[[Dimensions[MM][[1]], j]], InputForm] <> "#";
   }; (* last element *)
   strMM = strMM <> ToString[MM[[(Dimensions[MM][[1]]), Dimensions[MM][[2]]]], InputForm] <> "} right )";
strMM;
)
预期产出为:

"left ( matrix{3/2#-1#-2#-2#-2##0#3#6#10#14##-6#3/2#5#5#5##19/2#-7#-35/2#-24#-61/2} right )"
但它抛出了这个输出:

"left ( matrix{-61/2#-61/2##-61/2#-61/2} right )"
这不是预期的输出,但我无法找到错误


谢谢。

首先获取表示数组的字符串

我讨厌单字母变量名,我遵循Mathematica的惯例,对我定义的变量名使用小写字母开头),所以让我们

然后

返回所需的字符串

如果您想要一个函数来完成此操作,只需像这样将其全部粉碎:

ooForm[arr_List]:= StringReplace[ToString[arr,InputForm],
     {" " -> "", "," -> "#", "{{" -> "left ( matrix{", "}}"
     -> "} right)", "}" -> "#", "{" -> ""}]

你犯了一个对Mathematica新手来说很常见的基本错误。使用循环是一个明确的标志,表明您正在编写命令式、过程性的代码,这几乎总是对您的时间的低效利用(请注意,我编写的代码比您编写的代码要短得多,并且使用的函数也更少)和对计算机时间的低效利用。当然,后者不太重要,但如果您有兴趣,请比较一下您的方法和我的方法所花费的时间。

非常感谢您的回答,这非常有效。你是对的,我尝试将C风格的编程移植到Mathematica,就是这样。:)
myArray = {{3/2, -1, -2, -2, -2}, {0, 3, 6, 10, 14}, {-6, 3/2, 5, 5, 5}, {19/2, -7, -35/2, -24, -61/2}};
myArrayString = ToString[myArray,InputForm];
StringReplace[myArrayString,{" " -> "", "," -> "#", "{{" -> "left ( matrix{", "}}"
 -> "} right)", "}" -> "#", "{" -> ""}]
ooForm[arr_List]:= StringReplace[ToString[arr,InputForm],
     {" " -> "", "," -> "#", "{{" -> "left ( matrix{", "}}"
     -> "} right)", "}" -> "#", "{" -> ""}]