How to choose elements of a matrix with internally generated variables and rank them with real coefficients

I have a Code (a long one), part of it is below. The following piece basically multiplies an adjacency matrix with a vector of equilibrium values. Then tries to pick non-zero elements to be used in other sections of the Code. The entire Code is in DynamicModule with a Manipulate inside since I wanted to simulate the model with different random numbers.

My question is that, although I know my matrix a exists with positive numbers, I cannot extract those numbers and hence the entire Code fails.

The matrix a is given as follows:

a = {{0, 0, 0, 7.55596 FEalfa$$1563[4] FEtao$$1563[1], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7.55596 FEalfa$$1563[19] FEtao$$1563[1], 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.461227 FEalfa$$1563[14] FEtao$$1563[3], 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.665655 FEalfa$$1563[17] FEtao$$1563[4], 0, 0, 0}, {0, 0, 0, 0, 0, 3.14909 FEalfa1563[6] FEtao1563[5], 0, 0, 0, 0, 3.14909 FEalfa1563[11] FEtao1563[5], 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.34426 FEalfa1563[11] FEtao1563[6], 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1.66264 FEalfa1563[4] FEtao1563[8], .66264 FEalfa1563[5] FEtao1563[8], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0., 0, 0, 0, 0.}, {0, 0, 0, 0, 0, 0, 0, 4.32564 FEalfa1563[8] FEtao1563[10], 0, 0, 0, 4.32564 FEalfa1563[12] FEtao1563[10], 0, 0, 0, 0, 4.32564 FEalfa1563[17] FEtao1563[10], 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.02967 FEalfa1563[14] FEtao1563[11], 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 154.893 FEalfa1563[7] FEtao1563[13], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0.152293 FEalfa1563[6] FEtao1563[14], 0, 0, 0, 0, 0, 0, 0.152293 FEalfa1563[13] FEtao1563[14], 0, 0, 0, 0, 0.152293 FEalfa1563[18] FEtao1563[14], 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0., 0, 0, 0, 0, 0, 0., 0., 0, 0., 0., 0, 0, 0, 0., 0, 0, 0.}, {5.96944 FEalfa1563[1] FEtao1563[17], 0, 0, 0, 0, 0, 0, 0, 0, 5.96944 FEalfa1563[10] FEtao1563[17], 0, 0, 0, 0, 0, 0, 0, 5.96944 FEalfa1563[18] FEtao1563[17], 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1.93381 FEalfa1563[3] FEtao1563[19], 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.93381 FEalfa1563[13] FEtao$$1563[19], 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0., 0, 0, 0, 0., 0., 0., 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};  Now I try to extract all nonzero values with nonzeroes = Reap[Sow[#, # > 0] & /@ Flatten[a];, True][[2]] // Flatten;  But for some reason, this does evaluate to the empty list {}. Hope to receive some guidance. Thanks. • I also edited your question in order to boil it down to the essentials. Welcome at Mathematica.StackExchange! Sep 27, 2018 at 16:04 • Thank you very much. Excellent. Sep 27, 2018 at 16:06 • You're welcome. Sep 27, 2018 at 16:06 1 Answer I do not understand your question correctly. But by using SparseArray, you can extract nonzero positions and nonzero values as follows: spa = SparseArray[a];  Extracting the nonzero positions: spa["NonzeroPositions"] (* {{1, 4}, {1, 19}, {3, 14}, {4, 17}, {5, 6}, {5, 11}, {6, 11}, {8, 4}, {8, 5}, {10, 8}, {10, 12}, {10, 17}, {11, 14}, {13, 7}, {14, 6}, {14, 13}, {14, 18}, {17, 1}, {17, 10}, {17, 18}, {19, 3}, {19, 13}} *)  Extracting the nonzero values: spa["NonzeroValues"] (* {7.55596 FEalfa$$1563[4] FEtao$$1563[1], 7.55596 FEalfa$$1563[19] FEtao$$1563[1], 0.461227 FEalfa$$1563[14] FEtao$$1563[3], 0.665655 FEalfa$$1563[17] FEtao$$1563[4], 3.14909 FEalfa$$1563[6] FEtao$$1563[5], 3.14909 FEalfa$$1563[11] FEtao$$1563[5], 3.34426 FEalfa$$1563[11] FEtao$$1563[6], 1.66264 FEalfa$$1563[4] FEtao$$1563[8], 1.66264 FEalfa$$1563[5] FEtao$$1563[8], 4.32564 FEalfa$$1563[8] FEtao$$1563[10], 4.32564 FEalfa$$1563[12] FEtao$$1563[10], 4.32564 FEalfa$$1563[17] FEtao$$1563[10], 1.02967 FEalfa$$1563[14] FEtao$$1563[11], 154.893 FEalfa$$1563[7] FEtao$$1563[13], 0.152293 FEalfa$$1563[6] FEtao$$1563[14], 0.152293 FEalfa$$1563[13] FEtao$$1563[14], 0.152293 FEalfa$$1563[18] FEtao$$1563[14], 5.96944 FEalfa$$1563[1] FEtao$$1563[17], 5.96944 FEalfa$$1563[10] FEtao$$1563[17], 5.96944 FEalfa$$1563[18] FEtao$$1563[17], 1.93381 FEalfa$$1563[3] FEtao$$1563[19], 1.93381 FEalfa$$1563[13] FEtao1563[19]}
*)


If you insist on using Sow and Reap, then you should use UnsameQ as test for nonnullity as >, != and < won't evaluate in gereral if they are applied to expressions that involve symbols.

b = Reap[If[(# =!= 0) && (# =!= 0.), Sow[#]] & /@ Flatten[a], _][[2, 1]]

• Dear Henrik, You have already answered my question. I prepared a simple executable example to post with the answer you gave me. I am a new comer and I do not know if have any reputation to vote for your answer. I will try to give all my reputations, which only 1, I think. Furthermore, I do not know if I should post the working example with your input. I would appreciate if you let me know. Sep 27, 2018 at 15:49
• Okay, if this already settles your question: Fine. In order to accept my answer, just click the checkmark below the up- and downvote buttons. If you also click the upvote button, it will count as an upvote once you made the 15 reputation mark. The nice thing about stackexchange: Upvoting does not cost you any reputation. So you really don't have to "pay" other users: it is for free. (But everybody enjoys getting rep as sign of gratitude.) Sep 27, 2018 at 15:58
• I just noticed that "b" is not identical to spa["NonzeroValues"] because there is an extra "0" at the end of the b sequence. How can we remove that zero? Sep 28, 2018 at 11:18
• @Tebernus Godd point. I added a test against the floating point zero 0.. Sep 28, 2018 at 11:32
• it now works perfect. Tugrul Sep 28, 2018 at 12:12