Generating decimal values in matlab [duplicate] - matlab

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);

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

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

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,

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)

access to values in vector in matlab [duplicate]

This question already has answers here:
MATLAB - extracting rows of a matrix
(5 answers)
Closed 10 years ago.
I have a Nx2 vector, each row in the vector is a coordinate in a matrix.
For example: vector that call Path look like this:
Path=[1 2;
3 4;
5 6;
7 8;];
My question is how can I access the vector to take my x and y coordinates?
If I write Path(1) the answer is 1, and for Path(2) the answer is 3, But I want to take the pairs 1 2, then 3 4 etc..
Can I do it in a loop?
thanks!
This will give you every row one by one.
for i=1:size(Path,1)
Path(i,:)
end
If you just want to plot the path, try:
plot(Path(:,1),Path(:,2))