Creating arrays of unknown length

27 ビュー (過去 30 日間)
Maroulator
Maroulator 2015 年 12 月 30 日
コメント済み: Asif Khan 2020 年 11 月 22 日
I am trying to write a function that generates a sequence of random integer numbers between 1 and m, stopping when a value is repeated for the first time. The function would generate an array containing all the numbers generated except for the last value that is a repeated occurrence. For example, if the generated sequence is 3 1 9 5 7 2 5, the array to be returned should be 3 1 9 5 7 2. Below is the code that I have so far; I feel that I am close, but that I'm missing smth trivial. Any insight would be extremely appreciated.
function [v] = sequence_CM(m)
for i=1:m
A(i)=ceil(2+((100+100)*rand(1)));
B(i)=A(i);
for k=((i+1):length(B))
if B(i)==A(i+1)
break;
end
end
end
v=A;

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 30 日
Your code is treating m as a maximum length, with the generated values being integers in the range 3 to 202. But your description implies that m should be the maximum allowed random value -- which would also provide a natural maximum length by the pigeon hole principle (you cannot generate more than m different random integers in the range 1 to m without there being a duplicate.)
Your code is adding the new test value to both A and B, and is returning the full A. But when a duplicate is finally generated it is not to be put into the answer, according to your description.
It seems a waste of effort to maintain your B vector.
Hint: ismember(newvalue, A) and if it is not there then append newvalue to A, otherwise stop.
  2 件のコメント
Maroulator
Maroulator 2015 年 12 月 31 日
編集済み: Walter Roberson 2015 年 12 月 31 日
I simplified my code after taking your suggestion into account and now I have smth that looks like what I have below. Why is it that I only get a vector of 1s even after I override it in the for-loop? Any insight would be helpful; again, many thanks in advance.
A=ones(1,m);
for i=1:m
while ismember(A(i),A)==0
A(i)=floor(2+((100+100)*rand(1)));
end
end
v=A;
Walter Roberson
Walter Roberson 2015 年 12 月 31 日
A(i) is always already a member of A. Notice I referred to "newvalue" -- that is, generate a value without immediately storing it, and test to see if it is already there.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 12 月 31 日
You can use unique and check the length of it. If there are repeats, the length of hte unique array will be less than the original array. Not sure how fast it is, but it works.
m = 10 % whatever you want.
randomIntegers = randi(m, 1, m)
for k = 2 : length(randomIntegers)
% Get the array from index 1 up to index k.
thisArray = randomIntegers(1:k)
% Check if the number of unique numbers matches the length of the array.
% If it does, all are unique and there are no repeats.
% If it does not, then there must now be a repeat.
if length(unique(thisArray)) < k
% There must be a duplicate.
break
end
end
% Extract numbers just up to index (k-1) - we don't want the repeated integer.
randomIntegers = randomIntegers(1:k-1)
  1 件のコメント
Asif Khan
Asif Khan 2020 年 11 月 22 日
this is wrong

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


Asif Khan
Asif Khan 2020 年 11 月 22 日
thanks
  1 件のコメント
Asif Khan
Asif Khan 2020 年 11 月 22 日
you welcome

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by