Save real numbers (with decimals) in a loop into vector.

Dear All, I asked a question yesterday, but I wasn't so clear and believe that the information in the post got complicated and many would avoid looking at it~ I rephrase my problem very shortly:
Normally, if 'i' is a vector of positive integers, the vector of its product say 'i*rand' would be easy to get:
i=1:5;
y(i)=i*rand
y =
0.4468 0.8935 1.3403 1.7871 2.2339
But if 'i' is just a real number, say:
i=0.1:0.5;
y(i)=i*rand
Attempted to access y(0.1); index must be a positive integer or logical.
Question:
How do I overcome this problem and get 'y', the vector of the product 'i*rand'.
Thanks and Best of Regards// Ali

 Accepted Answer

ii=0.1:0.5;
counter=1:numel(ii)
y(counter)=ii*rand

7 Comments

Dear Azzi,
Many Many Thanks for the support//
I think I am almost there, but when I use the answer you just provided - I get just a scalar, that is just one value for y which stands for ii=0.1
ii =
0.1000
y =
0.0268
Any suggestions?
Sorry, it's
ii=0.1:0.1:0.5;
counter=1:numel(ii)
y(counter)=ii*rand
Dear Azzi, That did the trick for this, and so that is the answer - but it doesn't work with my function, I tried
s=str2double(get(handles.StockP,'String'));
counter=1:numel(s);
y(counter)=s
but it is giving again:
y =
101
y =
114.9920
y =
116.1000
y =
113.3770
y =
98.3933
If you help me overcome this, you would have saved my day :)
Thanks again//
It seems that the length of s is 1. What are you then expecting to have? Can you explain what is s and its size?
s is a stock price.
Suppose I set the number of simulation to 5:
At the beginning of the simulation, the first function will see if the number is > 0, set new value of number = n-1 (5-1) and call the second function which generates new stock price. At the end of the stock generating function, I call the first function and so the process will loop until n=0. Thus If I set n to 5, I would have 5 loops and so 5 different stock prices.
What I need is to record these prices in order to get averages, plot them and so on.
I tried several methods, some of which you proposed but they didn't work so far. The main idea is to get the last stock price in every loop, I do this by retrieving the string
s=str2double(get(handles.StockP,'String'))
but what I am not able to do is to put all these different prices in one vector instead of having them as separate entities such as
s =
117.0050
s =
100.0460
s =
96.5053
s =
99.0834
s =
114.7970
I need the result to be:
s =
117.0050 100.0460 96.5053 99.0834 114.7970
Any suggestions?
s=str2double(get(handles.StockP,'String'))
y=get(handles.StockP,'userdata')
y=[y s]
set(handles.StockP,'userdata',y)
Dear Azzi, Thanks heaps for your generous help// God Bless You
I can say that you saved my day :) and so the other question I raised yesterday is solved too.
The only thing is that it saves the array and everytime I start the function it adds the new values to the old values, but managed to overcome this by reseting the GUI.
Thanks again and all best// Ali

Sign in to comment.

More Answers (1)

You can simply omit the index on the left hand side:
ii = 0.1:0.5;
y = ii * rand
After this, you cannot access "y(0.1)", because index expressions must be integers. But you could create a function:
function r = y(x)
r = rand * x;
Now "y(0.1)" calculates the function for this value, but this is not exactly equivalent to the indexing in your example. But usually this is a sufficient solution.
When you really need to use a floating point value as index, you will get problems with FAQ: limited floating point precision:
any(0:0.1:1 == 0.3)
>> 0
This is not a bug, but cause by the fact, that not all decimal numbers can be represented exactly in binary format. Therefore any kind of floating-point-indexing would require a comparison like find(abs(v - value) < 100*eps). The limit of 100*eps is more or less arbitrary and depend on the values. In consequence the required code for indexing must be smart and need intelligent fallbacks.
Better stay on integer indexing.

2 Comments

You can then simply get an index vector with the same length as the non-integer indeces:
str = {'.1' '.2' '.3' '.4' '.5'};
ii = str2double(str);
y = ii*rand
ind = 1:length(y)
Thanks for your contribution Jan, much appreciated.
The function method does make sense, but in my particular case --> simulating stock price (please refer to my last comment), I tried creating the
function r=y(s)
where s, stock price, is the variable I am looking for, then I call the function y(s) in every loop, but the result is the same, just a scalar not a vector:
s =
117.0050
s =
100.0460
s =
96.5053
s =
99.0834
s =
114.7970
I wonder if you have any suggestions of how to put all these values into one vector?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!