MATLAB Answers

A Mathematica script that deals with multidimensional arrays needs to be converted to Matlab. How to do it?

1 view (last 30 days)
P Repetowicz
P Repetowicz on 12 May 2021
Commented: P Repetowicz on 13 May 2021
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}

Answers (1)

Walter Roberson
Walter Roberson on 12 May 2021
is a convertor from Mathematica code to MATLAB code.
  1 Comment
P Repetowicz
P Repetowicz on 13 May 2021
Thank you for that but maybe I didn't phrase my question properly. I was wondering if the whole mechanism of pattern matching and substitutions is available in Matlab. Let us take a simpler code snippet as an example. We have:
In[169]:= x =.; y =.;
mylist = {{1, 2, {{2, 3}, {6, 7}}}, {11, 3, {{1, 3, 5}, {5, 6, 7}}}};
mylist /. List[P_Integer, r_Integer, List[ss_List, tt_List]] :>
P Product[x[xi]^ss[[xi]] y[xi]^tt[[xi]], {xi, 1, r}]
Out[171]= {x[1]^2 x[2]^3 y[1]^6 y[2]^7,
11 x[1] x[2]^3 x[3]^5 y[1]^5 y[2]^6 y[3]^7}
As you can see we start witha list of lists and then we replace in there each entry by some function of that entry.
In Matlab we can of course code that list of lists as a cell array but is it possible to do the replacements? Ofcourse one could always apply cellfun to the cell array but this isn't really the same as in the example above.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!