Creating a random permutation that always start with a specific value

조회 수: 9 (최근 30일)
Dear all,
I want to create a vector of the size ten that starts with a the value one and the rest must be random. Therefore I wanted to do the following: N = [1 randperm(9)]. However, randperm only returns the values between 1 and 9 in such a case. Is there anyone who knows how this works or knows another function that could help me with my problem.
Kind regards, Thomas

채택된 답변

Birdman
Birdman 2018년 4월 5일
It sounds like randi function can help you. First argument specifies the interval that integer values will be generated. Second and third argument specifies the number of row and column respectively.
N=[1 randi([2 9],1,8]
  댓글 수: 2
Thomas Beerten
Thomas Beerten 2018년 4월 5일
Thanks for your answer. The problem with the answer is that it creates a vector that contains the same numbers since it creates random numbers. I wanted to use randperm such that each number is present only once in the vector. Such that you get a vector that looks the following: [1 2 3 4 5 7 6 8 9 10]. Do you may know if that is possible with the function randi?
Regards
Birdman
Birdman 2018년 4월 5일
No that is not possible with randi. You may use randsample as follows to uniquely create numbers:
randsample(1:10,10)
But the order will be random.

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

추가 답변 (1개)

Torsten
Torsten 2018년 4월 5일
N=[1 1+randperm(9)];
Best wishes
Torsten.

Community Treasure Hunt

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

Start Hunting!

Translated by