Generate 1 random number between -1 and 1 in Matlab [duplicate] - matlab

This question already has answers here:
Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?
(2 answers)
Closed 7 years ago.
I am new to matlab and I need to add one random number between -1 and 1 to the equation. I also need to generate a random number between -5 and 5.
I need float number not int

For a random double between -1 and 1:
a = 2*rand(1)-1;
And between -5 and 5:
b = 5*(2*rand(1)-1);
Best,

Related

What is dot multiplication [duplicate]

This question already has answers here:
Return Unique Element with a Tolerance
(7 answers)
Closed 5 years ago.
When multiplying matricies what is the difference when there is a dot there? Can this give different results
You can just use the built-in Matlab function uniquetol to obtain the unique values up to some tolerance in the array and ask the length of the returned array.
Example
A = [1+1e-11 2 3 4 1 2 3]; % generate an array with 4 unique values except for some tolerance
length(uniquetol(A, 1e-10)) % will return 4

4 random numbers totaling 100 [duplicate]

This question already has answers here:
Split a random value into four that sum up to it
(7 answers)
Closed 7 years ago.
I want to generate 4 random numbers between 1 and 100 so that the total adds up to 100. How do I do that?
say result1) 20,10,40,30
result2 ) 33,33,33,1
etc. Thanks
The way to go is:
Step 1: Generate four random numbers between 0 and 1
Step 2: Add these four numbers
Step 3: Divide each of the four numbers by the sum,
Step 4: Multiply by 100, and round to the nearest integer.

Generating decimal values in matlab [duplicate]

This question already has answers here:
Random numbers that add to 100: Matlab
(4 answers)
Closed 7 years ago.
How to generate a vector of lets say 5 decimal values such that their sum is 10.
for i=1:5
d(i)=rand;
end
1)I know this generates the vector, but how to include the condition?
2) can I generate negative numbers as well?
d = rand(5,1);
d = d * 10 / sum(d);

MATLAB: how can I generate 2 random numbers which sum is less than 1? [duplicate]

This question already has answers here:
Random numbers that add to 100: Matlab
(4 answers)
Closed 9 years ago.
Does anybody know how to generate two random numbers which sum is less than one?
I found only topics describing how to generate 2 random numbers which add up to 1 (trough normalization)
Generate the first random number, r1.
Generate a random number less than 1 to be the random sum.
Define r2 as (sum - r1).
Assuming you don't care too much about the distribution:
x = rand(2,1);
if sum(x)>1
x=x/2;
end
Modifying normKrumpe's answer, I'd suggest
Generate the sum as a random number.
Generate a random number r1 less than (or <=?) the sum.
r2 = sum-r1.

Matlab - sum all elements above current [duplicate]

This question already has an answer here:
matlab based program where output comprises of sum of rows of input
(1 answer)
Closed 8 years ago.
I am trying to implement a vectorised solution in matlab for adding all elements above the current element in a vector. For eg.
I have a vector a as follows
a =
1
2
3
4
I would like a vector b like
b =
1
3
6
10
I know this can be done very easily using a loop but I was wondering if there are indexing options which can allow me to do the same in matlab/ octave?
You can use the Cumulative Summation function (cumsum):
b = cumsum(a)