Creating random vector with 10 different lengths

6 ビュー (過去 30 日間)
Chelles Rees
Chelles Rees 2019 年 11 月 10 日
コメント済み: Chelles Rees 2019 年 11 月 10 日
I can't seem to ceate a random vector from 1-10k then 1-20k then 1-30k consectuively until 100k logging different values.
Here is the code I have so far:
inc=[10000 20000 30000 40000 50000 60000 70000 80000 90000 100000];
for l=1:length(inc) %creating a loop to go through the 10 values of inc
vec1 = randi(1,inc(l)); %geenrating random vector from 1-respective value of inc
Any help would be appreciated

採用された回答

Image Analyst
Image Analyst 2019 年 11 月 10 日
You need to tell it either one row or one column. Try this:
inc = [10000, 20000 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000];
maxValue = 100000; % Whatever you want the potential max value in the vector to be.
for k = 1 : length(inc) % Creating a loop to go through the 10 values of inc
% Generating random vector from 1-respective value of inc.
vec1 = randi(maxValue, 1, inc(k)); % or randi(maxValue, inc(k), 1);
fprintf('Made a vector with %d rows and %d columns.\n', size(vec1, 1), size(vec1, 2));
end

その他の回答 (1 件)

per isakson
per isakson 2019 年 11 月 10 日
Documentation on randi says:
X = randi(imax,n) returns an n-by-n matrix of pseudorandom integers drawn from the discrete uniform distribution on the interval [1,imax].
Thus, randi(1,inc(l)); will return a matrix of ones the same as ones(inc(l)).
vec1 is overwritten in each iteration of the loop. Is its value stored in another variable?

カテゴリ

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