I would like to find out if there is a function that can generate random numbers from a set of numbers in Matlab? For instance say I have the set [-1 1]. How can I generate numbers from that set? I have tried to use randi([-1,1]) but that obviously will generate the numbers -1,0 and 1 and I would like to generate numbers uniformly WITHOUT the 0 and only include -1 and 1. I apologise in advance if this seems like a trivial question but I can't seem to find the answer.
Thanks for any help you can provide.
Edit: I've found a simple solution for the above: (randi([0 1])*2) - 1. This only solves this problem and not a generation of numbers from a specified set.
Well, is there a function that can generate integers uniformly from the set 1:2, or, in general, from the set 1:n, where n is the number of elements in your set? (Yes)
If the above answer was yes, then can those numbers be used as an index into the generated set? (Yes)
A = [-1 1];
n = numel(A);
A(ceil(rand*n))
Will it sample randomly and uniformly? (Yes)
Generate a uniform random integer in the range 1 to N (where N is the size of the set). Then use that to index into the set.
>> S = sign(rand(1,10) - .5); S(S ~= 0)
ans =
1 -1 1 1 -1 1 -1 -1 1 -1
This is obviously specialized to your specific example, but the same general concept can be applied.
EDIT: Here is a more general example:
>> Set = [1 10 100]
Set =
1 10 100
>> Set(randi([1 3], 10, 1))
ans =
100 100 100 100 100 10 10 1 100 1
Related
i have a question regarding histc:
I choose the max and min of a sorted signal as my range.
ma = ssigPE(end);
mi = ssigPE(1);
range = mi:ma;
[bincountsO,indO2] = histc(ssigPE, range);
so the range i get back is:
range = [-1.097184703736132 -0.097184703736132 0.902815296263868]
my problem is that just 2 bins get develop, so bincountsO has 2 bins
and indO2 has values as 0, 1 and 2
What am I doing wrong? I guess I m using the range wrong. I read the text here:
http://de.mathworks.com/help/matlab/ref/histc.html#inputarg_binranges
but I don't get it.
The bin ranges tell you where do bins start and stop. So a value of [0 1 2 7]for example, will give 3 bins: [0 1] , [1 2] , [2 7]
In matlab if you do mi:ma it will create an array from the value mi to ma with a step of 1. With your values, that gives just 3 values, hence 2 bins. There are 2 ways of creating a given step size length vectors.
Step size if 100 as an example
range=mi:(ma-mi)/100:ma;
alternatively, and way clearer
range=linspace(mi,ma,100)
This question already has an answer here:
How do I generate a random vector (0,1) with a known probability in MATLAB
(1 answer)
Closed 7 years ago.
For example, how to randomly generate a 1-by-12 matrix that contains 8 0s and 4 1s?
Like this matrix [1 0 0 1 0 0 0 1 0 0 1 0]
And if I generate again, it returns a different matrix [0 0 1 0 0 1 1 0 1 0 0 0]
Create a vector that starts with the desired number of 1s followed by the desired number of 0s and the use randperm to shuffle it around:
n = 8;
m = 4;
M = [ones(m,1), zeros(n,1)];
M = M(randperm(numel(M)))
Or you can do it slightly differently: http://www.mathworks.com/matlabcentral/answers/83289-how-can-i-create-a-random-binary-matrix-with-a-specified-number-of-1-s-and-0-s
You can make a vector containing 12 zeros and use randsample to pick four numbers that you make one:
a = zeros(1,12);
a(randsample(12,4)) = 1;
Note: This requires the 'Statistics and Machine Learning Toolbox'.
If you do not have this, you will not be able to use randsample.
This one may work as well.
x = zeros(12,1);
tmp = rand(12,1);
[~,ind] = sort(tmp);
x(ind(1:4)) = 1;
Not sure if is better than the other examples, but it is one way to do it. I would say it is a similar solution as the one by fhdrsdg, but this one does not requires statistics toolbox. It mat also be possible that this solution may not have the same elegance as matlabs solutions may have though and randsample have more features than this example.
I'm reading some MATLAB trying to pick it up. The line below is probably rather simple but I do not understand it.
I understand length will give me the length of a vector, in this case a vector which is part of a struct, index_struct.data_incl.
The actual value of index_stuct.data_incl at run time is simply 1. What is confusing me is what is inside the brackets i.e. (index_struct.data_incl == 1)? I can't work out what this line is trying to do as simple as it may be!
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
try this (but think of x as your index_struct.data_incl:):
x = [1 4 5 13 1 1]
length(x(x==1))
ans =
3
It's just counting the number of elements of your x vector that are equal to 1
because x==1 evaluates to [1 0 0 0 1 1] and then using logical indexing x(x==1) evaluates to [1 1 1] whose length is 3;
It could have been written more simply as sum(index_struct.data_incl == 1)
If I dont see the code I can only guess..., but I guess that index_struc.data_incl should be a vector, with length n meaning that you have the option to read until n files, and all the values of the array should be 0 at the begining, and when you read a file you change the corresponding position in the vector index_struc.data_incl from 0 to 1. After some time you can see how many of these files you have read using
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
because it will give to you the number of 1 in the vector index_struct.data_incl.
I've got fixed numbers: -3, -1, 1, 3. How do I randomly generate a matrix like the following?
1 -1 -3 -1
3 -3 -3 3
3 3 1 -1
3 -3 3 -1
Use randi to create random index values into your vector of possible values:
x = [-3 -1 1 3]
y = randi(length(x),[5 5]);
y = x(y);
Although #nkjt's answer is probably the way to go, if you have the Statistics Toolbox you can simplify a little using randsample (with replacement):
result = NaN(3,6); %// define required size
result(:) = randsample([-3 -1 1 3], numel(result), true);
Or, if the original numbers are equally spaced as in your example, you can solve it in one line:
result = 2*randi(4,[3 6])-5; %// "2" and "5" as per your original values
You can use
randperm Random permutation
From the exercises in a book I am using to learn MATLAB:
Given x = [3 15 9 12 -1 0 -12 9 6 1],
provide the command(s) that will
A) set the values of x that are
positive to zero
B) set values that are multiples of 3
to 3 (rem will help here)
C) multiply the values of x that are
even by 5
D) extract the values of x that are
greater than 10 into a vector called
y
E) set the values in x that are less
than the mean to zero
F) set the values in x that are above the mean to their difference from the mean
Question a) will teach you the following elements:
find a function that returns indexes given a condition, in your case x>0
use indexing in order to set selected values in x to 0
to be continued ...
x = [3 15 9 12 -1 0 -12 9 6 1]
vi = (x < 0) % statement that returns a boolean, gives a vector like
% [0 0 0 0 1 0 1 0 0 0]
x(vi) = -x(vi) % does the operation (negating in this case) on the relevant
% values of x (those with a 1 from above)
Without actually doing your homework, they all follow the above pattern.
I agree with the comments to your question, that is not necessarily the right way to go if you really want to learn something.
As to answer your question, MATLAB has a fantastic function browser I strongly suggest you take a look at it. With well chosen keywords you can go a long way. :)