How to generate pattern randomly In MATLAB
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Davide Masiello
2022-3-14
编辑:Davide Masiello
2022-3-14
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 个评论
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
2022-3-13
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 个评论
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
2022-3-14
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
If so use the repmat function.
yy2 = repmat(y, 1, 4)
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
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)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!