I use Mathematica 7 to process a large dataset. A data set is a three-dimensional array of signed integers. Three levels can be considered corresponding to X points per shot, Y-scans per scan, and Z-scans for each set.
I also have a “nullifying” shot (containing X points, which are signs of fractions of integers) that I would like to subtract from each frame in the data set. Subsequently, I no longer need the original dataset.
How can I perform this conversion without creating new copies of the data set or its parts in this process? Conceptually, the data set is in memory, and I would like to scan each element and change it in this place in memory, without constantly copying it to another place in memory.
The following stand-alone code reflects all aspects of what I'm trying to do:
(* Create some offsetted data, and a zero data set. *) myData = Table[Table[Table[RandomInteger[{1, 100}], {k, 500}], {j, 400}], {i, 200}]; myZero = Table[RandomInteger[{1, 9}]/RandomInteger[{1, 9}] + 50, {i, 500}]; (* Method 1 *) myData = Table[ f1 = myData[[i]]; Table[ f2 = f1[[j]]; f2 - myZero, {j, 400}], {i, 200}]; (* Method 2 *) Do[ Do[ myData[[i]][[j]] = myData[[i]][[j]] - myZero, {j, 400}], {i, 200}] (* Method 3 *) Attributes[Zeroing] = {HoldFirst}; Zeroing[x_] := Module[{}, Do[ Do[ x[[i]][[j]] = x[[i]][[j]] - myZero, {j, Length[x[[1]]]} ], {i, Length[x]} ] ];
(Note: Aaron Honecker hat tip for method number 3.)
On my machine (3.17 GHz Intel Core2 Duo processor, 4 GB of RAM, 32-bit version of Windows 7), all three methods use approximately 1.25 GB of memory, with fairings # 2 and # 3 slightly better.
If I am not against loss of accuracy, wrapping N[ ] around the internal objects myData and myZero when they are created increases their size in memory by 150 MB initially, but reduces the amount of memory required for zeroing (using methods # 1- # 3 above) from 1 , 25 GB to 300 MB! This is my working solution, but it would be great to know how best to deal with this problem.