How can i apply the Differences command to a list of matrices? I don't want to use a do cycle.
Do[ΔM[k] = M[[k]] - M[[k - 1]], {k, 1, Length[M]}]
I tried with Map, MapThread etc etc, but i don't get the result!!
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityDifferences
should work directly on your list of matrices, assuming that each matrix has the same dimensions. Compare its output with the more verbose Table
equivalent:
SeedRandom[0];
M = RandomInteger[9, {5, 3, 3}];
Table[M[[k]] - M[[k - 1]], {k, 2, Length[M]}]
Differences[M]
{{{0, 2, -7}, {-2, 5, -4}, {-6, 8, 0}}, {{-2, 3, 7}, {4, -1, 8}, {-2, -2, -6}}, {{-5, 4, -5}, {1, 1, -2}, {6, 3, 6}}, {{9, -5, -2}, {4, 1, 2}, {1, -2, -6}}} {{{0, 2, -7}, {-2, 5, -4}, {-6, 8, 0}}, {{-2, 3, 7}, {4, -1, 8}, {-2, -2, -6}}, {{-5, 4, -5}, {1, 1, -2}, {6, 3, 6}}, {{9, -5, -2}, {4, 1, 2}, {1, -2, -6}}}
List
even if they do not advertise that they do, e.g. Sort[foo[3, 1, 2]]
. (Though that is in the Sort
documentation.) Therefore it is often worth trying a function directly on a non-List
expression to see what happens.
$\endgroup$
Jan 19, 2017 at 16:56