Skip to main content
1-Visitor
July 21, 2017
Solved

Is there a faster way to create an Array using a program loop?

  • July 21, 2017
  • 3 replies
  • 13239 views

I'm not sure what I'm doing wrong but Mathcad is taking far longer than I would expect to create simple arrays.

 

Goal: create an array with 86,400 rows, all values are 0

 

For example, I can make an array like that immediately with B[86400 := 0.

 

However if i try to do the same thing using a while or for loop it takes nearly a minute. 

 

C:] t<--86400

   ] j<--0

   ] while j<t+1

   ]  ] C[j <--0

   ]  ] j<--j+1

   ] C

 

Now the program I'm actually trying to write is a little more complicated but I need to write arrays/matrices using programs.  My current file takes 20 min to run which is far to much considering that the calculations are not intense.  I figure I must be making mathcad work harder than necessary.  What am I doing wrong?

Best answer by Werner_E

I guess your Powermatrix is a 86401 x 2 matrix. Thats not really big, indeed.

And you want to create a 49 x 3 matrix by averaging some values.

I retyped without doing any optimizations - just used a syntax to access matrix elements which I am more used to.

I even omitted the preallocation of the three vectors. Usually when dealing with big matrices thats a good idea but given that your three vectors are just 49 element vectors I guess the benefit will not be noticable.

As you can see in the screenshot it took about 30 ms to calculate your compressed matrix.

The calculation was done on a very old and lame notebook.

How long does it take youu to do the calculation? Or did I miss something?

Pic.png

 

 

3 replies

24-Ruby IV
July 21, 2017

Array60-400.png

asw3911-VisitorAuthor
1-Visitor
July 21, 2017

This is why I'm confused.  That matrix takes no time to generate.  However if I try to do it within a program block it takes 10000000x longer.  I need to create arrays within a program block.

21-Topaz II
July 24, 2017

the sum of an interval of PowerMatrix is defined diferently in a reply compared to your jpeg.

 

[PowerMatrix(1)](i)i  in other words it must extract the column of PowerMatrix for each iteration in the sum.

PowerMatrix( i,1)  is direct access and faster in one of the replies.

 

It is also better to define the upper boundary of this sum as a number rather than calculate it each time for each item in the sum.

23-Emerald I
July 21, 2017

Mathcad was  (I'm not sure if Prime kept it) optimized for vector/matrix operations.  The "vectorize function" that creates an arrow above the operation is far faster (for example) than multiplying two vectors element by element.  When you write a program to populate an array you force Mathcad to throw out that optimization and do element by element.

 

Without a more specific example I can't offer better guidance.  Investigate stack, augment, and vectorize to see if there is any way to reduce the amount of programming.

asw3911-VisitorAuthor
1-Visitor
July 21, 2017

Thanks for the reply.  I'll be more specific with what I'm trying to accomplish.  I've essentially modelled power (kW) generated for every second of a day (86,400 seconds).  I have to calculate it at this scale. 

 

I want to compress the data into 30 min intervals (1800 seconds).  1st column will be the time in min (30,60,90..) and 2nd column will be the average power generated within 30 min interval.  I've done this successfully, it just takes an excessive amount of time. 

 

I read somewhere that it was faster to create a matrix with all 0's then with a loop replace the values rather than just create the matrix with loop.  It didn't affect calculation time that way.  Attached is the block that takes 15-20min to run. 

23-Emerald I
July 21, 2017

Rather than create the large vector (86400 seconds) can you

  • calculate the first 30 seconds, take the average, write the first row of your "compressed" array,
  • calculate the next 30 seconds,
  •  
  • rinse and repeat

Filling a matrix with zeros then overwriting it sounds like a memory storage trick.

23-Emerald I
July 21, 2017

Try:

 

Prog(N) := | A[N,N <--0

                  return A

 

Works up to 16000 x 16000, and it's fast.