How can I efficiently store and access previous values in a MATLAB code?

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Code Matlab
sara_87
Messages
748
Reaction score
0

Homework Statement



I have a problem storing values in an array in Matlab.
If i want to return values according to the formula:
A(v,u)=v u^2-A(v-1,u-1)
where v is the row number and u is the coloumn number.

So, we need to know the previous elements in the matrix to calculate the next.
Please see the attempt below.

Homework Equations





The Attempt at a Solution


the code:

function A = trying1(n)
A(1,1)=0;
for v=2:n-2
for u=2:v
A(v,u)=v*u^2-A(v-1,u-1);
end
end

will give:

0, 0, 0, 0
0, 8, 0, 0
0, 12, 19, 0
0, 16, 24, 45

if we have n=6.

Fow n=7, we have:

0, 0, 0, 0, 0
0, 8, 0, 0, 0
0, 12, 19, 0, 0
0, 16, 24, 45, 0
0, 20, 29, 56, 80

(the first block is the same as n=6)
However if i put n=20, this will take a longer time because it caluculates the previous already calculated terms again.
I want to store the previous calculated ones so that this reduced the elapsed time (for very big n).

Any ideas or suggestions will be very much appreciated.
Thank you
 
Physics news on Phys.org
The number of terms that you've stored is very small compared to the number of terms to be calculated if you're jumping from n=7 to n-200. You could make trying1(n) a function trying1(n, oldA) instead, and just build your new A around your old A very quickly by setting newA(u,v)=oldA(u,v) for all relevant u and v but I doubt you would see much improvement

You might be able to get real speed improvement if you do less double for looping.
 
''You might be able to get real speed improvement if you do less double for looping.''
but since the new values depend on both u and v, how can i do this avoiding the double for loop?

Thank you :)
 
There are some functions in MATLAB that are designed to work around for loops. cumsum, cumulative sum, is the first one that comes to mind although there are others (if you google MATLAB vectorization the first hit is matlab's own tutorial on these functions. Be warned: some of the advice is out of date and wrong now, so check with tic tocs to make sure you're actually speeding up your code if you make changes). I don't have a solution for this specific example and maybe there isn't a good one, but it's worth thinking about for at least a couple of minutes if you want it to run faster.

Whether it's worth it or not is something that depends on how the code is being implemented. Is the problem that you're calculating trying1(n) for each value of n between 1 and 300 in order, in which case just saving old values is probably good enough (and maybe superior), or are you trying to calculate trying1(2000000) or something crazy?

Another option if it's just a lot of low number calculations is to just calculate it for some n, say 50, right at the start, and use that matrix as long as it's good enough. If you ever need a larger value, double the size of the matrix you need and calculate that until you need a bigger one. You can save yourself a lot of function evaluations this way
 
I am calculating trying1(n) for each value of n between 1 and 300 in order.

How do i save old values?
Do i need to make a function oldA for each n?

would i need something like this:

function oldA = trying1(n)
A(1,1)=0;
for v=2:n-2
for u=2:v
A(v,u)=v*u^2-A(v-1,u-1);
end
end

function newA = trying1(n)
for v=2:n-2
for u=2:v
A(v,u)=v*u^2-oldA(v-1,u-1);
end
end


thank you.
 
There are two things I don't understand about this problem. First, when finding the nth root of a number, there should in theory be n solutions. However, the formula produces n+1 roots. Here is how. The first root is simply ##\left(r\right)^{\left(\frac{1}{n}\right)}##. Then you multiply this first root by n additional expressions given by the formula, as you go through k=0,1,...n-1. So you end up with n+1 roots, which cannot be correct. Let me illustrate what I mean. For this...
Back
Top