6
$\begingroup$

Novice question! Does the dimensionality of a matrix affect the speed of operations performed on it (or the speed of retrieval of values from it) IF the total number of elements in the matrices are the same? E.g. is it computationally more/less efficient to use a 3600 x 120 matrix vs a 60 x 60 x 120 matrix?

The simulations I'm working with have to repeatedly use values from the matrix to do some calculations. I could have tried out for myself, but changing the dimensionality of my current matrix would need me to restructure most of my code to run a comparison.

In general, are lists of more dimensions more expensive memory-wise? Or is it just the number of elements that matters?

$\endgroup$

1 Answer 1

5
$\begingroup$

There's no appreciable difference in memory usage, for either unpacked or for packed arrays:

(* UNPACKED *)
array = RandomReal[1, {3600, 120}];
array[[1, 1]] = 0; (* unpack it *)
array // ByteCount
(*  10628096  *)

array = RandomReal[1, {60, 60, 120}];
array[[1, 1, 1]] = 0; (* unpack it *)
array // ByteCount
(*  10631584  *)

(* PACKED *)
array = RandomReal[1, {3600, 120}];
array // ByteCount
(*  3456160  *)

array = RandomReal[1, {60, 60, 120}];
array // ByteCount
(*  3456168  *)

There are other odd efficiencies of unpacked arrays, since they are stored as linked lists. For instance if a is an array, then b = {a, a} uses a link to a and entails hardly any more memory usage...until you change a component as in b[[2, 1]] = 0.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.