Choose random element of vector

조회 수: 162 (최근 30일)
giorgos kivides
giorgos kivides 2020년 1월 24일
댓글: giorgos kivides 2020년 1월 24일
I have a vector and i want to choose 2 random elements x1 and x2.
The x2 element I do not want to be the same element as x1 and i want to select after element x1
For example:
vector = [1,2,3,4,5,6,7,8,9]
element x1 may be the number 2. element x2 cannot be number 1 or number 2.

답변 (5개)

Sindar
Sindar 2020년 1월 24일
편집: Sindar 2020년 1월 24일
% select a random integer between 1 and the last index of the vector
ind_1 = randi([1 length(vector)]);
% select a random integer between ind_1 and the last index of the vector
ind_2 = randi([ind_1+1 length(vector)]);
% select the elements associated with these indices
x1=vector(ind_1);
x2=vector(ind_2);
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 1월 24일
If length(vector) is randomly choosen for ind_1 then ind_2 has no room to be chosen from.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2020년 1월 24일
P1 = randi(length(Vector) - 1);
P2 = randi([P1+1,length(Vector)]) ;
x1 = Vector(P1) ;
x2 = Vector(P2) ;
Note that this is not a fair distribution. You could get a fairer distribution if you dropped the requirement that x2 be chosen strictly after choosing x1:
P = sort(randperm(length(Vector, 2)));
x1 = Vector(P(1));
x2 = Vector(P(2));
This is fairer but violates the condition that x1 be chosen before x2.

James Tursa
James Tursa 2020년 1월 24일
편집: James Tursa 2020년 1월 24일
k = sort(randperm(numel(vector),2));
x1 = vector(k(1));
x2 = vector(k(2));
This assumes that you always want to be able to pick two numbers. I.e., x1 cannot be vector(end) and x2 cannot be vector(1).

giorgos kivides
giorgos kivides 2020년 1월 24일
Thanks a lot.I need some help. when choosing the two random elements, I would like to take the elements between them and change their order.
for example
vector = [1 2 3 4 5 6 7 8 9]
Random Elements: X1 = number 2. X2 = number 6.
the vector will have the format:
Newvector = [1 2 5 4 3 6 7 8 9]
  댓글 수: 2
James Tursa
James Tursa 2020년 1월 24일
To reverse order:
vector(k(1)+1:k(2)-1) = vector(k(2)-1:-1:k(1)+1);
To have random order:
m = randperm(k(2)-k(1)-1) + k(1) ;
vector(k(1)+1:k(2)-1) = vector(m);
giorgos kivides
giorgos kivides 2020년 1월 24일
thanks a lot

댓글을 달려면 로그인하십시오.


giorgos kivides
giorgos kivides 2020년 1월 24일
i want something yet.. i have an array and i want to convert to vector. i want in the vector the first elements to be the first row, etc.
for example:
array= [ 1 2 3 4 5; 11 12 13 14 15; 21 22 23 24 25]
the vector will have the format:
vector = [1 2 3 4 5 11 12 13 14 15 21 22 23 24 25]
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 1월 24일
reshape(array.', 1, [])
giorgos kivides
giorgos kivides 2020년 1월 24일
thanks. And i have the vector and i want to add the number 1 at start of vector. for example:
vector = [2 3 4 5 6]
newvector = [1 2 3 4 5 6].
how to add the number?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by