# Trying to add up values into a matrix

first post here.

I'm currently completely stuck and frustrated with mathematica. In retrospective, it might had been a mistake to use it for this, I'm actually considering to somehow export the results I have so far into python. Disregarding that: What I have right now are two nested lists/matrices, one of which is empty and is supposed to be filled by the nested list. The nested list is organized in such a way that each line has a value, and 2 positional values, to insert into the matrix. I've been trying to use while loops, for loops etc. and everytime it just returns an unchanged empty matrix to me.

Some "pseudo-code" I've been actually using:

Test1 = {{1, 1, 1}, {2, 3, 2}, {3, 1, 1}}
FinTest1 = ConstantArray[0, {3, 3}]
i = 1;
While[i < 4, Print[i];
j = 1;
While[j < 4,
n = 1;
While[n < 4,
If[i === Test1[[n, 3]] And j === Test1[[n, 2]],
FinTest1[[i, j]] = FinTest1[[i, j]] + Test1[[n, 1]]];
n++];
j++];
m++];
MatrixForm[FinTest1]


This one is a heavily simplified version of what I am currently doing (Mine has around a million of data and the matrix is around 50 times larger). As you might see, at least that's something I expected to get, is a matrix in the form of:

{{4,0,0},{0,0,2},{0,0,0}}


Instead I get the empty matrix back, as if nothing happened with it. I've also noticed that the iterables stay the same within each loop, meaning i is always 1 inside the inner-most While loop, even though it should iterate.

Mind you, these are just my calculations, that's all I've been using Mathematica for so far. It's the first time I use some kind of "proper code" in this. But it turns out that somehow it treats values inside the loops differently from normally. Is there a way to circumvent that?

• I'm making some assumptions, but maybe you're trying to do something like this: SparseArray[Rule[{#1, #2}, #3] & @@@ Test1, {3, 3}] Mar 30 at 16:46
• @lericr It's mostly experimental. Though it didn't seem to have an issue with it. Replace seems to be a wonderful function. Though now I'm having a problem with drawing a matrixplot/arrayplot. All I get is "Argument {Matrix} at position 1 is not a list of lists". Even though it is... Does Wolfram have a maximum matrix size which it is willing to plot? (360x180)
– Hewe
Mar 30 at 17:13
• that sounds like a new question. but you should trust the error message and try to fix that first. Mar 30 at 17:23
• @lericr Managed to find the mistake. Replace seems to cause the problem by turning the matrix into a ragged list (somehow a few elements simply seem to disappear). SpraseArray on the other hand seems to have a problem with decimal values (around 10^-4)
– Hewe
Mar 30 at 17:30

Instead of using While, it is simpler to process the Test1 variable as follows:

Test1 = {{1, 1, 1}, {2, 3, 2}, {3, 1, 1}};
FinTest1 = ConstantArray[0, {3, 3}];

Replace[Test1, {i_, j_, k_} :> AddTo[FinTest1[[j,k]], i], {1}];
FinTest1


{{4, 0, 0}, {0, 0, 0}, {0, 2, 0}}

It is also possible to modify SparseArray options to do this as well, e.g.:

old = SystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries"];
SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total];


Then, using SparseArray:

SparseArray[Replace[Test1, {i_, j_, k_} :> {j, k} -> i, {1}]] //Normal


{{4, 0}, {0, 0}, {0, 2}}

Finally, reset the SparseArray options:

SetSystemOptions[old];

• Thanks. It actually calculated it extremely fast, but there seems to be a problem with it removing some elements from the matrix, or at least making wolfram think that it's a ragged list which cannot be converted into an array nor matrix. SparseArray causes problems with the following error message: The left-hand side of {89,0}->0.000109064 in {{89, -29} (shortened) 0.0000853162, {90, 7} -> 0.0000215934} is not a position or a pattern \ that will match the position of an element in an array with depth
– Hewe
Mar 30 at 17:33
• Right, this is for the 360x180 matrix I'm using. I've checked the dimensions of the inputs, and they're correct, 360 180.
– Hewe
Mar 30 at 17:41
• Mathematica starts indexing matrices with 1, not 0. j and k must be positive integers. Mar 30 at 17:54
• Oh right. Thanks
– Hewe
Mar 30 at 18:21