How to generate pattern randomly In MATLAB

Hello Everyone, I hope you are doing well.
I have the following pattern , i want to generate this pattern randomly in Matlab.
Y axis values is upto 1000 and and X axis values is also 1000 ( thousand samples) ignore the 0.1 value
The current plot has three levels, i want this levels upto 16 which is generated randomly
for example i have three levels then i have three values which are repeated to complete 1000 samples like the plot below.
can anybody please help me

 採用された回答

Davide Masiello
Davide Masiello 2022 年 3 月 14 日
編集済み: Davide Masiello 2022 年 3 月 14 日

0 投票

This is another approach based on the comments under my previous answer.
It tries to implement the request by @Med Future that: it generate the the output y which is equal to number of levelsx1000 for eg. 10 level then shape is 10x1000. but i want the same 10x1000 into single row 1x1000 to generate that shape.
To be fair, I am still not sure this is what @Med Future is trying to achieve, but it might be worth a shot.
NOTE: the code below implements the routine by @Image Analyst to compute random values which have a minimum spacing requirement.
clear,clc
numRequired = randi(16,1,1); % However many y values you require.
y = zeros(1, numRequired);
minSpacing = 30; % Whatever.
maxIterations = 100000; % Way more than you think you'll ever need. Failsafe to prevent infinite loop.
loopCounter = 1; % Number of times the loop goes. includes keepers and rejects.
counter = 1; % Index of y
while counter <= numRequired && loopCounter < maxIterations
trial_y = 1000 * rand;
distances = abs(y - trial_y);
if min(distances) > minSpacing
% It's far enough away so keep it.
y(counter) = trial_y;
counter = counter + 1;
end
loopCounter = loopCounter + 1;
end
x = 1:1000;
y = repelem(y,floor(length(x)/length(y)));
% completes y up to 1000 values by repeating the start of y
if length(y) < 1000
y(end+1:1000) = y(1:1000-end);
end
plot(x,y,'-ob');

2 件のコメント

Med Future
Med Future 2022 年 3 月 14 日
  • @Davide Masiello you are doing great job Sir, this method is good, but shape of the pattern is change.
  • The pattern shape should be like above which i have shared.
  • rest of the method is what i wanted.
Davide Masiello
Davide Masiello 2022 年 3 月 14 日
So this?
clear,clc
numRequired = randi(16,1,1); % However many y values you require.
y = zeros(1, numRequired);
minSpacing = 30; % Whatever.
maxIterations = 100000; % Way more than you think you'll ever need. Failsafe to prevent infinite loop.
loopCounter = 1; % Number of times the loop goes. includes keepers and rejects.
counter = 1; % Index of y
while counter <= numRequired && loopCounter < maxIterations
trial_y = 1000 * rand;
distances = abs(y - trial_y);
if min(distances) > minSpacing
% It's far enough away so keep it.
y(counter) = trial_y;
counter = counter + 1;
end
loopCounter = loopCounter + 1;
end
x = 1:1000;
y = ones(size(x)).*y';
plot(x,y,'-ob');

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

その他の回答 (2 件)

Davide Masiello
Davide Masiello 2022 年 3 月 13 日

0 投票

clear,clc
n = 16; % Number of levels
L = randi(1000,n,1); % Level values
x = 1:1000;
y = ones(size(x)).*L;
plot(x,y,'-ob');

12 件のコメント

Med Future
Med Future 2022 年 3 月 13 日
@Davide Masiello Thanks for your answer, but i want random levels upto 16, in your above code we have only 16 level, can you please modified it,
for example in one plot i have two levels , in second plot i have 10 levels. and generate array of 1000 by 1000
Med Future
Med Future 2022 年 3 月 13 日
@Davide Masiello and also you can see some random values are closer to one another like one have 965 and other is 958, i want the values to be 30 to 40 % each other for example if i have 965, the other value should be 990
Image Analyst
Image Analyst 2022 年 3 月 13 日
"i want random levels upto 16," <= then make n random.
n = randi(16, 1, 1) % Number of levels anywhere from 1 to 16
n = 13
Med Future
Med Future 2022 年 3 月 13 日
@Image Analyst thanks, The code share above the output y shape depend on number of level for example if i have 10 levels the shape is 10x1000. but i want that 10x1000 in a single row. 1x1000.
and generate a random data which have 1000 rows and each rows have this kind of pattern with different levels.
Image Analyst
Image Analyst 2022 年 3 月 13 日
You can make them separated by a min separation like this. You can generate a trial y value and then get the distance to all the other y values and if it's far enough away, keep it, otherwise try a new y value.
numRequired = 11; % However many y values you require.
y = zeros(1, numRequired);
minSpacing = 30; % Whatever.
maxIterations = 100000; % Way more than you think you'll ever need. Failsafe to prevent infinite loop.
loopCounter = 1; % Number of times the loop goes. includes keepers and rejects.
counter = 1; % Index of y
while counter <= numRequired && loopCounter < maxIterations
trial_y = 1000 * rand;
distances = abs(y - trial_y);
if min(distances) > minSpacing
% It's far enough away so keep it.
y(counter) = trial_y;
counter = counter + 1;
end
loopCounter = loopCounter + 1;
end
y = sort(y)
y = 1×11
41.3635 140.5341 301.3178 356.8373 425.0306 546.3434 593.6049 632.5164 697.0209 888.9652 966.0485
Davide Masiello
Davide Masiello 2022 年 3 月 14 日
The code share above the output y shape depend on number of level for example if i have 10 levels the shape is 10x1000. but i want that 10x1000 in a single row. 1x1000.
and generate a random data which have 1000 rows and each rows have this kind of pattern with different levels.
Could you try to explai this a bit better?
When you say that you want a 10x1000 matrix in a single 1x1000 raw, I am not sure what you mean.
Rik
Rik 2022 年 3 月 14 日
You wanted randomly generated values that were sufficiently spaced, that is the result that Image Analyst provided. This code essentially generates the L variable from the original answer, but calls it y instead.
Med Future
Med Future 2022 年 3 月 14 日
@Davide Masiello let me explain it to you.
like the above code share by image Analyst. it generate the the output y which is equal to number of levelsx1000 for eg. 10 level then shape is 10x1000. but i want the same 10x1000 into single row 1x1000 to generate that shape.
secondly i want it random.
Med Future
Med Future 2022 年 3 月 14 日
@Rik but the shape has only single value, it does not gives the pattern i want like i share the picture above. which are equally spaced
Rik
Rik 2022 年 3 月 14 日
Look at the x-axis. There are exactly 16 values there. You wanted 16 random levels. The code Image Analyst wrote for you has 16 numbers as a result. What is your problem? You only need to use his code to generate the L matrix in the code Davide wrote for you:
n = 16; % Number of levels
L = GenerateLevels(n,30); % Level values
L=L(:);
x = 1:1000;
y = ones(size(x)).*L;
plot(x,y,'-ob');
function y=GenerateLevels(numRequired,minSpacing)
% numRequired : However many y values you require.
% minSpacing : Whatever.
y = zeros(1, numRequired);
maxIterations = 100000; % Way more than you think you'll ever need. Failsafe to prevent infinite loop.
loopCounter = 1; % Number of times the loop goes. includes keepers and rejects.
counter = 1; % Index of y
while counter <= numRequired && loopCounter < maxIterations
trial_y = 1000 * rand;
distances = abs(y - trial_y);
if min(distances) > minSpacing
% It's far enough away so keep it.
y(counter) = trial_y;
counter = counter + 1;
end
loopCounter = loopCounter + 1;
end
y = sort(y);
end
See?
Also, you said you wanted random numbers. How can random numbers be equally spaced?
Med Future
Med Future 2022 年 3 月 14 日
@Rik like you mention 16 levels, right , i want these 16 levels to be random , like sometime i get 14 sometime i get 4, levels should be random from 2 to 16
Rik
Rik 2022 年 3 月 14 日
Image Analyst also already provided that code for you:
n = randi(16, 1, 1) % Number of levels anywhere from 1 to 16
You can easily change that range to 2 to 16 like this:
n = 1 + randi(15, 1, 1) % Number of levels anywhere from 2 to 16

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

Steven Lord
Steven Lord 2022 年 3 月 14 日

0 投票

So to clarify you have a vector of y values:
y = [1 2 3];
that you want to repeat over and over to give a pattern like:
yy = [1 2 3 1 2 3 1 2 3 1 2 3] % etc
yy = 1×12
1 2 3 1 2 3 1 2 3 1 2 3
If so use the repmat function.
yy2 = repmat(y, 1, 4)
yy2 = 1×12
1 2 3 1 2 3 1 2 3 1 2 3
If instead you want each element of the resulting vector to contain one of the values from y chosen at random you can use the randi function.
n = numel(y);
ind = randi(n, 1, 12); % A 1-by-12 vector of random integers between 1 and n
yy3 = y(ind) % Use them as indices into y
yy3 = 1×12
3 2 2 2 2 1 2 3 3 3 2 1
Or if you want each element of y to be represented equally often, use randperm to shuffle the elements of yy2.
order = randperm(numel(yy2));
yy4 = yy2(order)
yy4 = 1×12
1 3 2 3 2 1 3 3 2 1 1 2
histogram(yy4) % Show the uniform distribution
If you mean something else, please describe in more detail what data you start with and what your ultimate goal is.

1 件のコメント

Med Future
Med Future 2022 年 3 月 14 日
@Steven Lord The first apporach, yy2 = repmat(y, 1, 4)
  • I want random levels 2 to 16,
  • then i want random values which is 30 to 40% away from the next value for example one values is 951 the next should be 990.
  • Then Repeat the matrix to complete 1000 samples .
  • First row consists of one pattern upto 1000 pattern to complete shape of 1000x1000

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

カテゴリ

ヘルプ センター および File ExchangeParticle & Nuclear Physics についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by