hai i need a command for following details........
Ältere Kommentare anzeigen
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........
1 Kommentar
Jan
am 11 Dez. 2017
Please post one thread per problem only. See: https://www.mathworks.com/matlabcentral/answers/372244-need-a-command-as-simple-things-sir
Akzeptierte Antwort
Weitere Antworten (2)
KL
am 11 Dez. 2017
Simpler:
Use randsample
randsample(2:12,4)
ans =
9 3 4 2
9 Kommentare
Birdman
am 11 Dez. 2017
Where is the limit in this case?
KL
am 11 Dez. 2017
2:12 is the limit.
randsample samples specified number of values(4) from the given vector. The given vector should contain the numbers within the limits (2 to 12).
Birdman
am 11 Dez. 2017
But for instance he wants to generate 4 numbers from 2 and 12 and wants the maximum from those 4 numbers to be 7. The problem is different.
But for instance he wants to generate 4 numbers from 2 and 12 and wants the maximum from those 4 numbers to be 7...
Where is it mentioned?
If the generated numbers should be from 2 to 12 but their maximum should be 7... well, I cannot wrap my head around this. Do you mean you want to generate numbers between 2 to 7?
Birdman
am 11 Dez. 2017
@KL: That was what I understand and it seems that it was what he wanted.
KL
am 11 Dez. 2017
You might need a loop or something like arrayfun,
result = cell2mat(arrayfun(@(x)randsample(2:12,4),1:7,'uni',0)')
result =
2 6 3 9
6 4 2 5
8 5 7 10
8 3 5 11
4 2 3 7
7 5 6 4
8 6 5 12
VIJAY
am 11 Dez. 2017
Jan
am 11 Dez. 2017
@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
am 11 Dez. 2017
"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 Kommentar
Jan
am 11 Dez. 2017
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.
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!