Generating random integer between negative and positive range in MATLAB [duplicate] - matlab

This question already has answers here:
Generate a random number in a certain range in MATLAB
(9 answers)
Closed 6 years ago.
I'm trying to generate a random integer in the range of -2 and 5 using round and rand functions. I'm able to generate a random integer however it always returns a negative value and a zero.
round(rand(1)*-5)

Use randi
r = randi([-2 5],1)
rand
And if you want to do this only using rand and round
Try this: r = round(rand(1)*7 - 2);

heres also a possible way of doing it:
% generate 1000 random integers between -2 and 5
R = ceil(rand(1000,1)*8)-3;
% display MIN / MAX
disp(min(R));
disp(max(R));

Related

Matlab 2013a index error [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 6 years ago.
I got exactly this error:
Attempted to access E(3,1); index must be a positive integer or logical.
But the index is E(3,1), those numbers are both positive. What is going on?
for t=T:0.2:4
for i=1:N
% D = D +1
x = randi(Nsamples,1,1);
if(x==1)
Etemp = E(t*5,i) - S(x)*S(x+1) + (-S(x))*S(x+1);
elseif(x==Nsamples)
Etemp = E(t*5,i) - S(x)*S(x-1) + (-S(x))*S(x-1);
else
%********************* This is the error line
Etemp = E(t*5,i) - (S(x-1)*S(x)+S(x)*S(x+1))+ (S(x-1)*(-S(x))+(-S(x))*S(x+1));
end
end
end
3 index in E(3,1) may not exactly be an integer. In your case, index row index 3 is generated by multiplying t*5 i.e. 0.6*5 (if t=0.6). It does not guarantee it to be an integer.
In a high accuracy check on generated index value 3, you will find that it is off from the exact integer 3 by 1 bit or so at its least significant end.
Therefore, while indexing E(3,1), 3 is not perceived as an integer.
In cases where you generate the index by multiplying with a decimal, make sure to convert it to int before using it for indexing such as round(t*0.5) or int8(t*0.5).
Or all together avoid index which are generated by multiplying the decimals.

Compare displayed digits of double in Matlab [duplicate]

This question already has answers here:
What are the best practices for floating-point comparisons in Matlab?
(5 answers)
Closed 8 years ago.
I need to compare two double values in Matlab and check if they are equal. Now the two compared values are displayed to the user, so he can check the printed result, if necessary.
Now I need to know: Is it possible to compare the two double values so that they are equal, if their decimal representation (using 15 significant digits) is equal? For performance reasons, I would prefer not to compare the resulting strings.
For example the two hexadecimal values 3fd04b2bcf617348 and 3fd04b2bcf617359 represent the same displayed number and should therefore be treated equal, whereas 3fd04b2bcf617348 and 3fd04b2bcf617347 have different decimal representations and should be treated as different, even if their difference is lower:
fprintf('eq: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359'))
fprintf('ne: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347'))
You can round the values created by hex2num to 15 digits. This is done using the round function.
For MATLAB R2014b and higher, you can specify the precision directly
b = round(a,15);
For older versions, round just rounds to integers, so you will have to do it manually:
b = round(10^15 .* a) ./ 10^15;
If you compare the numbers after rounding, you get the desired behaviour:
a = [ hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359') ; ...
hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347') ];
% Round to 15 digits
b = round(10^15 .* a) ./ 10^15;
% Compare results
abs(b(1,1) - b(1,2)) < 4*eps(b(1,1))
ans =
1
abs(b(2,1) - b(2,2)) < 4*eps(b(2,1))
ans =
0

reflecting random walk in matlab? [duplicate]

This question already has answers here:
Matlab -- random walk with boundaries, vectorized
(2 answers)
Closed 8 years ago.
I have a array of 10 vectors 'x' with as below (for simulating 1D random walk):
r=rand(10,1000);
r(r>.5)=1;
r(r<=.5)=-1;
x=cumsum(r);
The image of the one vector will be like:
If I consider 2 values in the sequence , say +10 and -10, then I would like to reflect the sequence 'x' when it reaches those values. How to achieve this?
Before answering your question, a should point that your code is broken. By default cumsum accumulate data across first dimension, to change this behavior you should specify dim parameter:
x = cumsum(r,2);
And, answering your question, you could simply invert all data above threshold:
threshold = 10;
nsteps = ceil( max(abs(x(:))) / (2*threshold) - 0.5 );
for ii = 1:nsteps
ind = abs(x) > 10;
x(ind) = 20 * sign(x(ind)) - x(ind);
end

generating a random value between 0.001 and 0.0015 [duplicate]

This question already has answers here:
Matlab, matrix containing random numbers within specified range
(3 answers)
Closed 8 years ago.
I want to generate random values between 0.001 and 0.0015 such that each time I run a for loop, i get a new value.
e.g
value = random number between 0.001 and 0.0015;
for i = 1:10,
for value,
Calculate something...
end
end
Can any one tell me how to do that?
It's all written in the documentation of rand() function: http://www.mathworks.com/help/matlab/ref/rand.html
Example 1 Generate values from the uniform distribution on the
interval [a, b]:
r = a + (b-a).*rand(100,1);
So in your case a = 0.001 and b = 0.0015 and you can also change rand(100,1) to just rand(10,1) to give you 10 random values on the interval [a,b].
It's simple, you just do:
0.001+(rand()*(0.0015-0.001))
Here is how you should be doing it in my opinion:
myNumbers = rand(10,1)*0.0005+0.001;
for value = myNumbers;
%Calculate something
value
end
Note that the main improvement here is that you precalculate all random values at once.
Furthermore you can loop over them directly.

Random selection from datasource [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
randperm subset of random m-by-n matrix
When trying to randomly select 1000 rows from a dataset "fulldata" I get an error, im not very sure how its done in matlab but this is what I tryed:
data = datasample(fulldata,6,1000)
The fulldata is 490256x6 and I want to select 1000 random rows from this data. In matlab its throwing the error
??? Undefined function or method 'datasample' for input arguments
of type 'double'.
Error in ==> randomselection at 44
data = datasample(fulldata,6,1000)
You could instead to something like this to get it without relying on datasample:
pointsToPick = 1000; %# Numbers to pick
rVec = randperm(N); %# Random permutation of datapoint indices (N=490256 in your case)
randomSample = fulldata(rVec(1:pointsToPick),:); %# Random sample