Could anyone help me how to solve the following issue.

1 回表示 (過去 30 日間)
jaah navi
jaah navi 2019 年 5 月 28 日
コメント済み: jaah navi 2019 年 5 月 28 日
I am using the command line
randi([1,5], 1, 10)
to generate numbers with respect to single row versus 10 columns.
when i run the command sometimes it generates all 5 numbers with respect to single row versus 10 columns in the following manner
3 1 2 1 1 4 4 5 2 5
sometimes it doesnt and i am getting the result as follows:
4 1 5 5 5 1 1 2 2 4(number 3 is missing)
But for me i want to have all 5 number every time when the command executes.Could anyone please help me on this.
  2 件のコメント
Rik
Rik 2019 年 5 月 28 日
Come on Jaah, you must have tried something already. This is not your first question. Show your attempt.
jaah navi
jaah navi 2019 年 5 月 28 日
I tried with the command randi([1,5], 1, 10) as i am unable to get the required result i posted the question.

サインインしてコメントする。

採用された回答

Rik
Rik 2019 年 5 月 28 日
I can't think of a better solution, but you could try brute-forcing it with a while loop:
maxval=5;cols=10;
if maxval>cols
error('impossible combination')
end
result=randi(maxval,1,cols);
val_list=1:maxval;
while ~all(ismember(val_list,result))
result=randi(maxval,1,cols);
end

その他の回答 (2 件)

Renato SL
Renato SL 2019 年 5 月 28 日
From
help randi
you will get the following:
R = randi([IMIN,IMAX],...) returns an array containing integer
values drawn from the discrete uniform distribution on IMIN:IMAX.
Since the numbers are drawn with uniform distributions, I don't think randi is what you should use. One clue is given in later lines:
The arrays returned by randi may contain repeated integer values. This
is sometimes referred to as sampling with replacement. To get unique
integer values, sometimes referred to as sampling without replacement,
use RANDPERM.
Perhaps try using randperm instead to have all 5 numbers everytime the command executes, as you expect.
PS.
Maybe this is not the most efficient answer, but try using this line:
[randperm(5,1) randperm(5,2) randperm(5,3) randperm(5,4)]

KSSV
KSSV 2019 年 5 月 28 日
[randi([1,5], 1, 5) randi([1,5], 1, 5)]
  2 件のコメント
Rik
Rik 2019 年 5 月 28 日
This is not guaranteed to contain all numbers 1:5.
jaah navi
jaah navi 2019 年 5 月 28 日
Ok.For repeating 5 numbers with respect to 10 columns i can use your command.
But if i need to repeat 100 numbers with respect to 300 columns i am unable to use the above command.
So i need to have to a common command which gets executed for all possibilities
Could you please help me on this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by