A Mathematica script that deals with multidimensional arrays needs to be converted to Matlab. How to do it?
2 次查看(过去 30 天)
显示 更早的评论
We have a three dimensional array the entries of which are Lists of Lists the later of which have a particular structure as below.
List[P, r, List[ss_List, tt_List]]
For each entry of our 3 d array we want to perform a certain operation on this entry, the operation being as follows.
For each entry of the List of Lists we calculate a number fct[entry] and then we sum up those results over the List.
There are basically three (four) ways of doing it, firstly by mapping our function fct[] onto the array at an appropriate level, secondly by using pattern matching and substutions and thirdly simply by using a For[] loop.
Below I test those three methods and they of course lead to the same results.
In addition, the execution time, is in all four cases roughly the same.
My question would the following. Firstly, How do I export the array ft1[[]] to Matlab?
Secondly, how do I code all those four different approaches in Matlab (especially the three first ones because the last approach is of course trivial).
And @@ Flatten[
Map[MatchQ[#, List[P_Integer, r_Integer, List[ss_List, tt_List]]] &,
Drop[ft1, 1], {4}]]
MatchQ[xx, List[__Real]] && MatchQ[yy, List[__Real]]
fct[ml_List] :=
ml[[1]] Fold[Times, 1,
Table[(xi! xx[[xi]])^ml[[3, 1]][[xi]] (xi! yy[[xi]])^
ml[[3, 2]][[xi]], {xi, 1, ml[[2]]}]];
{t0, myres0} = Timing[Map[Total[#] &, Map[fct[#] &, ft1, {4}], {3}]];
{t1, myres1} =
Timing[Map[Total[Map[Function[x, fct[x]], #]] &, ft1, {3}]];
{t2, myres2} =
Timing[ ft1 /. (List[P_Integer, r_Integer,
List[ss_List, tt_List]] :> fct[List[P, r, List[ss, tt]]]) /.
List[ml__Real] :> Total[List[ml]]];
t3 = AbsoluteTime[];
myres3 = Table[
0, {p, 1, Length[ft1]}, {q, 1, Length[ft1[[p]]]}, {m, 1,
Length[ft1[[p, q]]]}];
For[p = 1, p <= Length[ft1], p++,
For[q = 1, q <= Length[ft1[[p]]], q++,
For[m = 1, m <= Length[ft1[[p, q]]], m++,
myres3[[p, q, m]] = Total[fct[#] & /@ ft1[[p, q, m]]];
]]];
t3 = AbsoluteTime[] - t3;
Total[Abs@Flatten[(myres0 - myres1)]]
Total[Abs@Flatten[(myres1 - myres2)]]
Total[Abs@Flatten[(myres2 - myres3)]]
{t0, t1, t2, t3}
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!