13 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
source | link

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
source | link
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
source | link
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
source | link
9 deleted 55 characters in body
source | link
8 added problem
source | link
7 forgot to one sentence
source | link
6 added another tag
| link
5 fixed dimension of final array
source | link
4 fixed dimension of final array
source | link
3 added parameters so code can run
source | link
2 added 269 characters in body
source | link
1
source | link