13 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/ edited Apr 13 '17 at 12:55 I am trying to optimize my code that searches for elements in the rows of matrix that are greater than 0. Then use that position to grab the element from other matrix. I use the do loop to iterate through each row. Then have another function that creates a new vector to replace that row of the vector. I have read these two questions on stackexchange (Question 1Question 1,Question 2Question 2) that say to use Map, Scan or NestList instead of for loops, but I am having difficulty applying it to my program. Any suggestions on how to speed this up would be greatly appreciated. Everyone says not to use for loops, which is what I originally had so switch to a Do but it has the exact same time to evaluate. Not sure how to use the suggestion in (Question 1Question 1,Question 2Question 2) for my iteration. Any advice would be greatly appreciated. I have many other numerical iterations using Euler's Algorithm that I have the same issue with. Update: I have tried to do the same thing as thisthis numerical method on stack exchange but doesn't help with my row replacement. I have tried MarcoB's suggestion but has gotten very convoluted to to the dot product and of the ir and iAB which is a giant array of arrays of different sizes. Ill update my code i have tried with that soon. I am trying to optimize my code that searches for elements in the rows of matrix that are greater than 0. Then use that position to grab the element from other matrix. I use the do loop to iterate through each row. Then have another function that creates a new vector to replace that row of the vector. I have read these two questions on stackexchange (Question 1,Question 2) that say to use Map, Scan or NestList instead of for loops, but I am having difficulty applying it to my program. Any suggestions on how to speed this up would be greatly appreciated. Everyone says not to use for loops, which is what I originally had so switch to a Do but it has the exact same time to evaluate. Not sure how to use the suggestion in (Question 1,Question 2) for my iteration. Any advice would be greatly appreciated. I have many other numerical iterations using Euler's Algorithm that I have the same issue with. Update: I have tried to do the same thing as this numerical method on stack exchange but doesn't help with my row replacement. I have tried MarcoB's suggestion but has gotten very convoluted to to the dot product and of the ir and iAB which is a giant array of arrays of different sizes. Ill update my code i have tried with that soon. I am trying to optimize my code that searches for elements in the rows of matrix that are greater than 0. Then use that position to grab the element from other matrix. I use the do loop to iterate through each row. Then have another function that creates a new vector to replace that row of the vector. I have read these two questions on stackexchange (Question 1,Question 2) that say to use Map, Scan or NestList instead of for loops, but I am having difficulty applying it to my program. Any suggestions on how to speed this up would be greatly appreciated. Everyone says not to use for loops, which is what I originally had so switch to a Do but it has the exact same time to evaluate. Not sure how to use the suggestion in (Question 1,Question 2) for my iteration. Any advice would be greatly appreciated. I have many other numerical iterations using Euler's Algorithm that I have the same issue with. Update: I have tried to do the same thing as this numerical method on stack exchange but doesn't help with my row replacement. I have tried MarcoB's suggestion but has gotten very convoluted to to the dot product and of the ir and iAB which is a giant array of arrays of different sizes. Ill update my code i have tried with that soon. 12 added 68 characters in body edited Jun 20 '16 at 16:18 MTR 6966 bronze badges dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] c = SortBy[GatherBy[positions, Last], Last@*Last][[;; , ;; , 1]] rij = Select[DeleteCases[0] /@ Transpose[dataMatrix], UnsameQ[#, {}] &] f = Function[{n, m}, n.m]; ddAB = Map[(iB[[#, All]]) &, rij]c] MapThread[f, {rij, ddAB}]  dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] rij = Select[DeleteCases[0] /@ Transpose[dataMatrix], UnsameQ[#, {}] &] f = Function[{n, m}, n.m]; dd = Map[(iB[[#, All]]) &, rij] MapThread[f, {rij, dd}]  dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] c = SortBy[GatherBy[positions, Last], Last@*Last][[;; , ;; , 1]] rij = Select[DeleteCases[0] /@ Transpose[dataMatrix], UnsameQ[#, {}] &] f = Function[{n, m}, n.m]; AB = Map[(iB[[#, All]]) &, c] MapThread[f, {rij, AB}]  11 deleted 151 characters in body edited Jun 20 '16 at 15:48 MTR 6966 bronze badges dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] crij = SortBy[GatherBy[positions, Last],Select[DeleteCases[0] Last@*Last][[;;/@ Transpose[dataMatrix], ;; UnsameQ[#, 1]]{}] &] f = Function[{n, m}, n.m]; dd = Map[(iB[[#, All]]) &, c]rij] MapThread[f, {crij, dd}]  So I have gotten this but the c function gives me the form of Rij but the values are the values of the row and not the actual dataMatrix values. And still have trouble dealing with a column that has all zeros. Update: In some good news if I can over come the c function equaling the the Rijzero elements it takes the time down from 69.1293 to 36.64. Not great but something!!! Open for any suggestions!!! dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] c = SortBy[GatherBy[positions, Last], Last@*Last][[;; , ;; , 1]] f = Function[{n, m}, n.m]; dd = Map[(iB[[#, All]]) &, c] MapThread[f, {c, dd}]  So I have gotten this but the c function gives me the form of Rij but the values are the values of the row and not the actual dataMatrix values. And still have trouble dealing with a column that has all zeros. Update: In some good news if I can over come the c function equaling the the Rij elements it takes the time down from 69.1293 to 36.64. Not great but something!!! Open for any suggestions!!! dataMatrix = RandomInteger[{0, 2}, {3, 4}]; iA = RandomReal[{-1, 1}, {3, 2}]; iB = RandomReal[{-1, 1}, {4, 2}]; positions = Position[dataMatrix, _?(# > 0 &)] rij = Select[DeleteCases[0] /@ Transpose[dataMatrix], UnsameQ[#, {}] &] f = Function[{n, m}, n.m]; dd = Map[(iB[[#, All]]) &, rij] MapThread[f, {rij, dd}]  So I have gotten this but still have trouble dealing with a column that has all zeros. Update: In some good news if I can over come the zero elements it takes the time down from 69.1293 to 36.64. Not great but something!!! Open for any suggestions!!! 10 update of timing edited Jun 20 '16 at 8:47 MTR 6966 bronze badges 9 deleted 55 characters in body edited Jun 20 '16 at 8:38 MTR 6966 bronze badges 8 added problem edited Jun 20 '16 at 8:28 MTR 6966 bronze badges 7 forgot to one sentence edited Jun 20 '16 at 8:07 MTR 6966 bronze badges 6 added another tag | link edited Jun 20 '16 at 6:14 MTR 6966 bronze badges 5 fixed dimension of final array edited Jun 16 '16 at 18:51 MTR 6966 bronze badges 4 fixed dimension of final array edited Jun 16 '16 at 18:45 MTR 6966 bronze badges 3 added parameters so code can run edited Jun 16 '16 at 17:51 MTR 6966 bronze badges 2 added 269 characters in body edited Jun 16 '16 at 17:44 MTR 6966 bronze badges 1 asked Jun 16 '16 at 17:14 MTR 6966 bronze badges