# Filtering the elements of an array to split them into two categories

So, I am new to Mathematica, and I am trying to write something that involves taking values from one array and separating them into two different arrays. My code for doing this is:

For[i = 1, i < (TruncationOrder + 1), i++,
(If[(Abs[Im[evalueb1[[1, i]]]] < 10^-14),
(If[Re[evalueb1[[1, i]]] > 0,
ρplus[[i, i]] = evalueb1[[1, i]],
ρminus[[i, i]] = evalueb1[[1, i]]]),
If[Im[evalueb1[[1, i]]] > 0,
ρplus[[i, i]] = evalueb1[[1, i]],
ρminus[[i, i]] = evalueb1[[1, i]]]])];


However, this does not give the desired results. I randomly have '1' several times in my new array. The actual values are given below:

Original:

 {-0.651301 - 2.44576*10^-16 I, 0.651301 + 3.73803*10^-17 I, 0.671298 - 6.68417*10^-16 I,
-0.671305 + 1.17248*10^-17 I, -0.000107956 + 0.735512 I, -0.000107956 -
0.735512 I,
9.93642*10^-6 + 0.764605 I, 9.93642*10^-6 - 0.764605 I, -0.965926 + 5.29506*10^-16 I,
0.965926 + 3.76737*10^-16 I, 0.97048 + 4.33453*10^-16 I, -0.97048 + 5.39299*10^-16 I,
0.0344992 - 1.4214 I, 0.0344992 + 1.4214 I, 0.0028631 + 1.4505 I, 0.0028631 - 1.4505 I,
-0.0626803 + 1.78014 I, -0.0626803 - 1.78014 I, -0.12443 + 1.98252 I, -0.12443 - 1.98252 I,
0.167724 - 2.24691 I, 0.167724 + 2.24691 I}


New incorrect ones:

ρplus:

    {1, 0.651301 + 3.73803*10^-17 I, 0.671298 - 6.68417*10^-16 I, 1, -0.000107956 + 0.735512 I,
1, 9.93642*10^-6 + 0.764605 I, 1, 1, 0.965926 + 3.76737*10^-16 I,
0.97048 + 4.33453*10^-16 I}


ρminus:

    {-0.651301 - 2.44576*10^-16 I, 1, 1, -0.671305 + 1.17248*10^-17 I,
1, -0.000107956 - 0.735512 I, 1, 9.93642*10^-6 - 0.764605 I,
-0.965926 + 5.29506*10^-16 I, 1, 1}

-
What is "original"? evalueb1? –  rm -rf May 21 at 16:28
since pplus pminus evalub1 are 2d arrays, what you are showing for original and incorrect isn't any of them –  george2079 May 21 at 17:07

It looks like you pre-initialized your ρplus and ρminus lists with a vector of ones, which is why you see them in the result at positions where the conditions don't satisfy. Presumably, you come from a MATLAB/procedural programming background, and I suggest you read this post (and all other answers on that question) and this one and familiarize yourself with the different list manipulation and iterating functions before proceeding.

As for your problem, you can write it neatly in a Mathematica style using Select as:

With[{TruncationOrder = 5, absTest = Abs@Im@# < 10^(-14) &, reTest = Re@# > 0 &, imTest = Im@# > 0 &},

pplus = Select[evalueb1[[;; TruncationOrder]],
(absTest@# && reTest@#) || (Not@absTest@# && imTest@#) &];

pminus = Select[evalueb1[[;; TruncationOrder]],
(absTest@# && Not@reTest@#) || (Not@absTest@# && Not@imTest@#) &];
]

-
Sorry if this is a dumb question, but what do the @ sign and the # sign mean? –  yankeefan11 May 21 at 17:01
It means you should have a look at the fine manual! And this as well...! ;) –  cormullion May 21 at 17:02
@cormullion Searching for @ returns a single reference which has to do with output formatting, and it is not immediately clear that f@x is equivalent to f[x]. Some interpretation is required to achieve that. –  rcollyer May 21 at 17:14
@rcollyer True enough (and where's Misery to complain about the documentation when we need him...?). But I know that some basic familiarity with Mathematica syntax is usually expected here, so I thought I'd hint as much... –  cormullion May 21 at 17:18
@cormullion on some level expecting some familiarity with the syntax is expected. However, we all had to learn it somewhere, and it took me longer than I care to admit to find this tutorial on shorthand notations. Of course, it seems to contain a number of things, so it is a good link, in general. –  rcollyer May 21 at 18:55