필터 지우기
필터 지우기

hai i need a command for following details........

조회 수: 1 (최근 30일)
VIJAY
VIJAY 2017년 12월 11일
편집: Jan 2017년 12월 11일
Here i am using the following command
as
>> randperm(12,4)
>> 1 3 8 12
In above command the maximum value is 12 for this command.
How can i set the the limit of randperm command ie the result should be as the range of 2 to 12???????????
the answer may be as
>>2 5 6 7........

채택된 답변

Birdman
Birdman 2017년 12월 11일
One approach:
while true
a=randperm(12,4)
if max(a)>7
else
break;
end
end
  댓글 수: 4
KL
KL 2017년 12월 11일
Unfortunately no...
Yes, there is. Check my answer.
Jan
Jan 2017년 12월 11일
편집: Jan 2017년 12월 11일
Why complicated if it can be written simpler? Compare:
if max(a) > 7
else
break;
end
with
if max(a) <= 7
break;
end
But the original question does not contain a limit like max(a) <= 7, but "the range of 2 to 12".

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

추가 답변 (2개)

KL
KL 2017년 12월 11일
Simpler:
Use randsample
randsample(2:12,4)
ans =
9 3 4 2
  댓글 수: 9
VIJAY
VIJAY 2017년 12월 11일
Thank you........
Jan
Jan 2017년 12월 11일
@Birdman: I do not see any limit of 7 for the values in the question also. "Values from 2 to 12 with maximum 7" is not even meaningful, but this would be "values from 2 to 7".

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


Walter Roberson
Walter Roberson 2017년 12월 11일
"s it possible to create an array by using this command(randsample(2:12,4)) for example the array size is 7*4 etc etc...???????"
[~,temp] = sort(rand(7, 11), 2);
result = 1 + temp(:,1:4));
The range 2 to 12 is achieved by taking the range 1 to 11 and adding 1 to that.
The random ordering is done by sorting random numbers by rows. This method of sort() is what randperm it self used to use.
  댓글 수: 1
Jan
Jan 2017년 12월 11일
randperm uses the sorting of a random vector only, if it is called with 1 input. For 2 inputs I assume the faster Fisher-Yates-Shuffle is applied (at least randperm(n,n) is much faster than randperm(n) - enhancement request was sent already).
Nevertheless, randperm(11, 4) + 1 is an easy solution also, but must be called in a loop.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by