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

In summary, the person is trying to find a way to store previous values so that the calculation of the next term is faster.
  • #1
sara_87
763
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
  • #2
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.
 
  • #3
''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 :)
 
  • #4
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
 
  • #5
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.
 

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

1. How can I optimize my code to make it run faster in MATLAB?

There are several ways to speed up your code in MATLAB. One option is to use vectorization, which involves performing operations on arrays instead of individual elements. Additionally, using built-in functions instead of writing your own can also improve speed. Another option is to preallocate memory for your variables, as this can save time by avoiding repeated resizing operations. You can also consider using parallel computing techniques, such as using the Parallel Computing Toolbox, to distribute your code across multiple processors for faster execution.

2. What is the best way to identify bottlenecks in my code that are causing it to run slowly?

One way to identify bottlenecks in your code is to use the MATLAB Profiler. This tool can help you pinpoint specific lines or functions in your code that are taking the most time to execute. It can also show you the number of times each line or function is called, as well as the amount of time spent in each one. This can help you identify areas for optimization.

3. How can I improve the efficiency of my loops in MATLAB?

One way to improve the efficiency of your loops is to avoid using nested loops when possible. Nested loops can significantly slow down your code, so try to find ways to vectorize your operations instead. Additionally, you can preallocate memory for your variables and use the "for" loop instead of the "while" loop, as it is often faster in MATLAB.

4. Are there any built-in functions in MATLAB that can help me speed up my code?

Yes, there are several built-in functions in MATLAB that can help you optimize your code for speed. For example, the "bsxfun" function can help you perform operations on arrays without using loops, which can improve efficiency. The "repmat" function can also be useful for creating arrays with repeated values, which can save time and memory compared to using a loop. You can also check the documentation for specific functions to see if there are any recommended ways to improve their efficiency.

5. Can I use parallel computing techniques in MATLAB to speed up my code?

Yes, MATLAB has built-in support for parallel computing, which can help you distribute your code across multiple processors for faster execution. This can be especially useful for computationally intensive tasks that can benefit from using multiple cores. You can also use the Parallel Computing Toolbox to take advantage of features such as parallel for-loops and distributed arrays. However, keep in mind that not all code is suitable for parallelization, so it is important to carefully consider if and how you can use parallel computing techniques in your specific case.

Similar threads

  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
636
  • Calculus and Beyond Homework Help
Replies
9
Views
1K
  • Calculus and Beyond Homework Help
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
14
Views
796
  • Calculus and Beyond Homework Help
Replies
2
Views
605
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
773
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
231
Back
Top