How to sort a random input numbers

4 ビュー (過去 30 日間)
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 11 日
コメント済み: Rainaire Hansford 2019 年 6 月 28 日
Hello Matlab Community
I have a code where I can input a set of numbers (3) and a random generator that is suppose to match which ever 3 numbers I pick in the given range.
To test this I set my code up like so:
clc
k=1;
s=3;
p=3;
x=0;
code = input('three numbers')
times=0;
while x<1
[~, Stemp] = sort(rand(k, s), 2);
S = Stemp(:, 1:p)
x=any( ismember(code,S, 'rows') )
times=times+1;
end
times=times
I would use [ 1 2 3] and it will run smoothly. However I still trying to test if it works so I only kept it 3. When I run it the answer S will give me number between 1 and 3 in any order but stop when it is all in the right order of example:
#> [1 2 3]
code =
1 2 3
S =
2 1 3
x = 0
S =
2 3 1
x = 0
S =
1 3 2
x = 0
S =
1 2 3
x = 1
times = 4
I would like it to stop when it just has all three numbers no matter which order. Is there a way to do that? Please provide example. Thank you.
  4 件のコメント
Adam Danz
Adam Danz 2019 年 6 月 25 日
編集済み: Adam Danz 2019 年 6 月 25 日
I don't understand "I would like it to stop when it just has all three numbers no matter which order. ".
It will always have 3 numbers because s = 3.
Also, if the user inputs [3,4,5] the loop will never end. Can you explain what your goal is and wehre you're stuck?
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 28 日
Ok Adam,
So "s" is equal to 3. Which means it will only give me number from 1 to 3.
"S" will give me a matrix 1 2 and 3 in it in random order. Ex. [1,2,3] or [1,3,2] etc. My loop continues when x is less then 1 but it turns one when my "S" is in the same matrix order as my input ("code"). I dont want that. I want x to equal 1 whenever all 3 number show in any order.
So if I changed "s" to 4 then "S" will still give me 3 numbers because "p" is 3 but "S" can show 3 numbers between 1-4.
All im trying to do is get a matching number in any order. so if I have [3 4 2]
And "S" gives out [4 2 3] x will equal 1 and the loop stops. Does that make sense?

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

回答 (1 件)

Vaibhav Tomar
Vaibhav Tomar 2019 年 6 月 26 日
Hey, what do you exactly mean by "Like to stop it all when it just has all three numbers no matter which order" Doesn't it initally or will certainly have three numbers as per your code. And the loop doesn't really seem to terminate as per the code and the aim required.
  2 件のコメント
Adam Danz
Adam Danz 2019 年 6 月 26 日
This comment should be moved to the comment section under the question since it is not an answer.
Rainaire Hansford
Rainaire Hansford 2019 年 6 月 28 日
So "s" is equal to 3. Which means it will only give me number from 1 to 3.
"S" will give me a matrix 1 2 and 3 in it in random order. Ex. [1,2,3] or [1,3,2] etc. My loop continues when x is less then 1 but it turns one when my "S" is in the same matrix order as my input ("code"). I dont want that. I want x to equal 1 whenever all 3 number show in any order.
So if I changed "s" to 4 then "S" will still give me 3 numbers because "p" is 3 but "S" can show 3 numbers between 1-4.
All im trying to do is get a matching number in any order. so if I have [3 4 2]
And "S" gives out [4 2 3] x will equal 1 and the loop stops. Does that make sense?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by