I want to generate 5 different random variables, & I want also to satisfy other condition which is N(rand1,rand2) =0 where N is 10-by-10 matrix that contains 0s & 1s.
This is the code that I wrote , it generate different random number , but I want to satisfy the other condition.
nb_sources=5;
nb_Des=5;
rand_nb= randperm(n,n);
source = [rand_nb(1:nb_sources)] ;
distination= [rand_nb(nb_sources+1:nb_sources+nb_Des)] ;
Since you're interested only in N(r1,r2)=0, you need to enumerate all these elements of N (lets say from from 1 to 30), generate 5 random numbers as rand(30,5,1) and pick up the indices. E.g. something like this
Nelem = 5;
[I,J] = find(N==0);
ind = randperm(size(I,1));
Res=[I(ind(1:Nelem)),J(ind(1:Nelem))];
Related
i wrote this code for randomize and round numbers
x=3+5*rand(3,4);
for n=1:3
for m=1:4
y(n,m)=round(x(n,m));
end
end
y
Using the randperm() function may be an option. The first argument of randperm() sets the range. In this case randperm(6,4) will generate 4 numbers that are within the range 1 to 6 (a random permuatation of integers in this case permutations of 6). If we add 2 to this result we can generate an array of length 4 that will have values ranging from 3 to 8. Here we can use one for-loop and generate the rows upon each iteration.
Array = zeros(3,4);
for Row = 1: 3
Array(Row,:) = randperm(6,4) + 2;
end
Array
First of all the rand() function returns numbers between 0 and 1, so it probably doesn't make sense to use this function and then round the numbers off. If you're looking for random integers use randi() instead.
With this in mind, the following code produces a 3 by 4 matrix filled with random integers and no repeats:
maxInteger = 12; %change to any number greater than 3x4 = 12
y = randi(maxInteger, 3, 4);
used = [];
for i = 1:numel(y)
while sum(find(used == y(i)))>0
y(i) = randi(maxInteger);
end
used = [used, y(i)];
end
If the while loop takes too long (as might happen with large matrices) consider filling a matrix by pulling and removing elements from a predecided list of integers.
I have a 102-by-102 matrix. I want to select square sub-matrices of orders from 2 up to 8 using random column numbers. Here is what I have done so far.
matt is the the original matrix of size 102-by-102.
ittr = 30
cols = 3;
for i = 1:ittr
rr = randi([2,102], cols,1);
mattsub = matt([rr(1) rr(2) rr(3)], [rr(1) rr(2) rr(3)]);
end
I have to extract matrices of different orders from 2 to 8. Using the above code I would have to change the mattsub line every time I change cols. I believe it is possible to do with another loop inside but cannot figure out how. How can I do this?
There is no need to extract elements of a vector and concatenate them, just use the vector to index a matrix.
Instead of :
mattsub = matt([rr(1) rr(2) rr(3)], [rr(1) rr(2) rr(3)]);
Use this:
mattsub = matt(rr, rr);
Defining a set of random sizes is pretty easy using the randi function. Once this is done, they can be projected along your iterations number N using arrayfun. Within the iterations, the randperm and sort functions can be used in order to build the random indexers to the original matrix M.
Here is the full code:
% Define the starting parameters...
M = rand(102);
N = 30;
% Retrieve the matrix rows and columns...
M_rows = size(M,1);
M_cols = size(M,2);
% Create a vector of random sizes between 2 and 8...
sizes = randi(7,N,1) + 1;
% Generate the random submatrices and insert them into a vector of cells...
subs = arrayfun(#(x)M(sort(randperm(M_rows,x)),sort(randperm(M_cols,x))),sizes,'UniformOutput',false);
This can work on any type of matrix, even non-squared ones.
You don't need another loop, one is enough. If you use randi to get a random integer as size of your submatrix, and then use those to get random column and row indices you can easily get a random submatrix. Do note that the ouput is a cell, as the submatrices won't all be of the same size.
N=102; % Or substitute with some size function
matt = rand(N); % Initial matrix, use your own
itr = 30; % Number of iterations
mattsub = cell(itr,1); % Cell for non-uniform output
for ii = 1:itr
X = randi(7)+1; % Get random integer between 2 and 7
colr = randi(N-X); % Random column
rowr = randi(N-X); % random row
mattsub{ii} = matt(rowr:(rowr+X-1),colr:(colr+X-1));
end
I have generated random numbers in MATLAB within a range using the below:
N=10000;
n=3000;
c=randperm(N,n);
I need another set of random numbers within the same range 1:N and of the same size n but excluding the values in c.
Any ideas?
You can use again randperm excluding the integers of array c to crate array d with same length n:
ok = 1:N;
ok(c) = [];
d=ok(randperm(numel(ok),n));
I want to create two random integers on the interval [1,n] which are guaranteed to be different from each other. I feel like
ri(1)=randi([1 n]);
ri(2)=randi([1 n]);
while ri(1)==ri(2)
ri(2)=randi([1 n]);
end
is not really the smoothest thing you can do.
One method is to use randperm so that you generate a random permutation of n values that are enumerated from 1 up to and including n, and only return the first two elements of the result:
ri = randperm(n, 2);
Older versions of MATLAB do not support calling randperm this way. Older versions only accept the one input variant, which by default returns the entire permutation of the n values. Therefore, you can call randperm using the one input version, then subset into the final result to return what you need:
ri = randperm(n);
ri = ri([1 2]);
Use randperm to create two unique values in range 1...n
out = randperm(n, 2)
out(1) = number 1
out(2) = number 2
If you wish to include 0's in your range. then:
out = randperm(n+1, 2);
out = out-1;
out(1) = number 1
out(2) = number 2
Here's another way:
ri(1) = randi([1 n]); % choose ri(1) uniformly from the set 1,...,n
ri(2) = randi([1 n-1]); % choose ri(2) uniformly from 1,...,n-1
ri(2) = ri(2) + (ri(2)>=ri(1)); % transform 1,...,n-1 into 1,...,ri(1)-1,ri(1)+1,...,n
I would like to generate three sets of random variables and I dont want them to be the same
for example if I want to generate the following sets ranged from 1 to 10
Set1= [ 1 4 ]
set2= [ 3 5 ]
Set3= [ 7 9]
You can create an array of unique random numbers using the function "randperm" and then divide the array into sets.
x = randperm(10,6);
Set1 = x(1:2);
Set2 = x(3:4);
Set3 = x(5:6);
After choosing two elements for each set, it's essential to randomize the remaining elements to choose from for the next iteration . The code below is trying to achieve it -
set123 = zeros(3,2); %// Pre-allocate for the output
array1 = 1:10; %// Array of numbers to choose from for the first iteration
for k = 1:3
%// Create many possible combinations of such two numbers from the sets.
%// The sets would have 10 elements to choose from at the start and
%// then 8 and then 6.
combs = nchoosek(array1,2);
%// At each stage all the possible combinations of the pairs must be
%// juggled and one of them must be selected randomly and then indexed
%// to get the set for this iteration
choosen_rowind = randperm(size(combs,1),1);
set123(k,:) = combs(choosen_rowind,:);
%// Setup the array of numbers left to choose from the next iteration
array1 = setdiff(array1,set123);
end
%// set123 is the desired output in a single array
If I understand correctly, you want to take r-times-c random variables (in your case r=3, c=2) from the set {m,m+1,...,n} (in your case n=1 and m=10), without repetition. I assume you want a (jointly) uniform distribution.
For that you use randsample:
result = reshape(randsample(m:n, r*c), r, c);