Saving data in a loop into a vector (values are not integers)
10 views (last 30 days)
Show older comments
Hello Everyone,
I am facing a little problem and would be very thankful if you can help me with it.
I have a function to calculate a stock price. It can loop up to 40 times to get the final price. I wanted to simulate the function and came into two problems:
1- I added a new variable, say x, that stands for the number of simulations. Now, at the beginning of the function, if x > 0, the main algorithm starts and loops over until x = 0. The problem is when it comes to saving the different stock prices at each run. Suppose x = 5, then I have 5 stock prices which I can see in Matlab command window. Suppose the prices "S" are [100.5 101.3 99.80 97.6 102.3], I can see it as: S = 100.5 ..... S = 102.3 but I want to save them all in one vector; I couldn't use y(S) as S is not an integer! any suggestions?
2- I was aiming to do some big simulation with the model, but noticed that I reached the recursion limit. Since up to 40 loops are required at each simulation/ do you think it is still possible to achieve this goal, say with 10,000 simulations? if yes, any initial guidance for this?
Thanks & Best Ali
4 Comments
Matt J
on 3 Jan 2013
Edited: Matt J
on 3 Jan 2013
Thanks for the point, it should have been clarified. The main 40 loops don't call for recursive call, but if I set x, the number of simulation, to any value above 25; that is above 25 simulations of the model which contains the 40 loops, then there will be a recursive call.
There's still no way we (those of us who have never seen your code) can understand why that occurs or why we should expect it to break the recursion limit.
Accepted Answer
Azzi Abdelmalek
on 4 Jan 2013
Edited: Azzi Abdelmalek
on 4 Jan 2013
If you want to put several numbers in one vector then create a counter
ii=0
for s=0::0.1:2
ii=ii+1
y(ii)=s
end
4 Comments
Azzi Abdelmalek
on 4 Jan 2013
Edited: Azzi Abdelmalek
on 4 Jan 2013
Use this
ii=0.1:0.5;
counter=1:numel(ii)
y(counter)=ii*rand
(avoid using i, it's reserved to complex numbers)
Other thing, you have to save your y variable, like you saved ii
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!