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
Click For Summary

Homework Help Overview

The discussion revolves around efficiently storing and accessing previous values in a MATLAB code, specifically related to a matrix calculation defined by a recursive formula. The original poster seeks to optimize the performance of their function as the input size increases.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants discuss the potential for modifying the function to accept previously calculated values to improve efficiency. There are suggestions to reduce the use of nested loops and explore MATLAB functions that may help with vectorization. Questions arise about how to effectively store and retrieve old values without redundant calculations.

Discussion Status

Participants are actively exploring different strategies for optimizing the MATLAB code. Some have provided suggestions for alternative approaches, while others are questioning the feasibility and implications of these changes. There is no explicit consensus on a single method, but the dialogue indicates a productive exploration of ideas.

Contextual Notes

The original poster is calculating the function for a range of values from 1 to 300 sequentially, which raises concerns about performance as the input size increases. There is an emphasis on the need to manage previously computed values effectively.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
14
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K