Page 1 of 1

Tips or tricks for a specific element by element operation

Posted: Wed Jul 22, 2015 5:10 am
by CharlieEVIEWS
Dear all,

I was wondering if anyone could provide any tips or tricks to speed up the efficiency of the operation of element by element operations. Please consider the following short program as an example:

Code: Select all

wfcreate u 1 'preamble to set up the question !size = 100 'preamble to set up the question matrix(!size,!size) XMAT 'preamble to set up the question matrix(!size,!size) YMAT = 0 'preamble to set up the question for !a = 1 to !size 'preamble to set up the question for !b = 1 to !size 'preamble to set up the question XMAT(!a,!b) = @nrnd 'preamble to set up the question next 'preamble to set up the question next 'preamble to set up the question for !a = 1 to !size for !b = 1 to !size if XMAT(!a,!b)<0.1*@max(XMAT) then YMAT(!a,!b)=XMAT(!a,!b) endif next next
As you can see from the comments, im specifically interested in the second half of the code. As !size gets large, this operation begins to take an extremely long time. That is, for all the elements of a 10k,10k matrix, it has to check whether a condition is met a hundred million times, and if so, fill a cell in a second matrix. If the condition isnt met, naturally, it's unchanged.

Does anyone have any ideas on how to speed this kind of thing up? Ive considered some kind of matrix multiplication method with resizing through the 16 decimal place structure, but cant figure out much, or think of anything. As this forum has been a rich treasure chest of tricks for computational efficiency in the past, I would try my luck here, before resorting to another programming language (which might be vectorized like YMAT= XMAT(XMAT > 0.1*max(Xmat)) in another language).

Best wishes!

Charlie

Re: Tips or tricks for a specific element by element operati

Posted: Wed Jul 22, 2015 10:16 am
by EViews Gareth
How about a 13,040x speed up?

Code: Select all

wfcreate u 1 'preamble to set up the question !size = 400 'preamble to set up the question matrix(!size,!size) XMAT 'preamble to set up the question matrix(!size,!size) YMAT = 0 'preamble to set up the question for !a = 1 to !size 'preamble to set up the question for !b = 1 to !size 'preamble to set up the question XMAT(!a,!b) = @nrnd 'preamble to set up the question next 'preamble to set up the question next 'preamble to set up the question tic for !a = 1 to !size for !b = 1 to !size if XMAT(!a,!b)<0.1*@max(XMAT) then YMAT(!a,!b)=XMAT(!a,!b) endif next next scalar time1 = @toc 'Gareth's go tic matrix(!size, !size) test = 0.1*@max(xmat) matrix ymat2 = @emult(@elt(xmat, test),xmat) scalar time2 = @toc

Re: Tips or tricks for a specific element by element operati

Posted: Wed Jul 22, 2015 11:14 am
by CharlieEVIEWS
Had no idea of that function. They should introduce a hand clapping emoji into the forums just for you, Gareth! :idea: