I need 25 different numbers between 1 and 34 which will select 25 different cases in a switch. Any help would be greatly appreciated.

1 回表示 (過去 30 日間)
Hey Everyone, I need to make a random number generator that does not repeat values and outputs a scalar which will then select a case inside a switch. I have tried using randperm but it outputs a double. In order for me to use the switch function, I need either a scalar or a string. I have tried converting to a string but it puts all of the numbers into one cell and I am not sure how to separate the numbers into different cells.
I need 25 different numbers between 1 and 34 which will select 25 different cases in a switch. Any help would be greatly appreciated.
  1 件のコメント
Stephen23
Stephen23 2015 年 5 月 26 日
It is quite likely that using a switch is not going to be the most efficient and tidy way to code this.
If you gave a bit more information about exactly what is happening inside the switch statement, then we might be able to suggest neater, less buggy and faster ways of achieving the same thing.
For example the entire list of random numbers could be used as indices to access the data inside a numeric or cell array. It all depends on what you are actually trying to achieve. Please tell us more details!

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

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2015 年 5 月 26 日
[~,ii] = sort(rand(34,1));
out = ii(1:25);

Thorsten
Thorsten 2015 年 5 月 26 日
編集済み: Thorsten 2015 年 5 月 26 日
r = randperm(34); % all number from 1 to 34 in random order
r = r(1:25); % 25 different numbers between 1 and 34
You can then use
switch r(i)
for any i between 1 and 25. r(i) is a scalar double that is valid in a switch statement.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by