3
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ To start with, your syntax for And is wrong. $\endgroup$
    – lericr
    Mar 30 at 16:41
  • $\begingroup$ I'm making some assumptions, but maybe you're trying to do something like this: SparseArray[Rule[{#1, #2}, #3] & @@@ Test1, {3, 3}] $\endgroup$
    – lericr
    Mar 30 at 16:46
  • $\begingroup$ @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) $\endgroup$
    – Hewe
    Mar 30 at 17:13
  • $\begingroup$ that sounds like a new question. but you should trust the error message and try to fix that first. $\endgroup$
    – lericr
    Mar 30 at 17:23
  • $\begingroup$ @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) $\endgroup$
    – Hewe
    Mar 30 at 17:30

1 Answer 1

4
$\begingroup$

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];
$\endgroup$
4
  • $\begingroup$ 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 $\endgroup$
    – Hewe
    Mar 30 at 17:33
  • $\begingroup$ Right, this is for the 360x180 matrix I'm using. I've checked the dimensions of the inputs, and they're correct, 360 180. $\endgroup$
    – Hewe
    Mar 30 at 17:41
  • $\begingroup$ Mathematica starts indexing matrices with 1, not 0. j and k must be positive integers. $\endgroup$
    – Carl Woll
    Mar 30 at 17:54
  • $\begingroup$ Oh right. Thanks $\endgroup$
    – Hewe
    Mar 30 at 18:21

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.