How to generate Pattern in MATLAB

21 次查看(过去 30 天)
Stephen john
Stephen john 2022-3-16
评论: Jon 2022-3-17
Hello everyone, I hope you are doing well.
I have the following pattern as shown in the image.
I want to generate this kind of pattern, my Y axis has random value from 1 to 1000 and X-axis has also same random 1 to value from 1000.
How can i generate it .

回答(3 个)

Jon
Jon 2022-3-16
编辑:Jon 2022-3-16
For the first plot you show, you could do something like this (you can adjust the exact scaling)
x=linspace(0,0.1)
xr = 0.1/3
y = mod(x,xr)*(850 - 410)/xr + 410
plot(x,y,'o')
ylim([300,900])
For the second plot the slopes are negative and the lines start at 850 so
y = mod(x,xr)*(410 - 850 )/xr + 850
  4 个评论
Stephen john
Stephen john 2022-3-17
@Jon i want 1000 samples in my array how can i do that?
Jon
Jon 2022-3-17
Replace the first line in the code above with the following
x = linspace(0,0.1,1000)

请先登录,再进行评论。


Scott MacKenzie
Scott MacKenzie 2022-3-16
编辑:Scott MacKenzie 2022-3-16
Here's code to generate the patterns in the image posted:
f = figure('color', 'w', 'Units','normalized','Position',[.25 .3 .4 .3]);
tiledlayout(1,2);
nexttile;
y1 = repmat(linspace(420, 850, 25),1,3);
x1 = linspace(0, 0.1, 75);
p = plot(x1,y1, 'o', 'MarkerSize', 4);
xlabel('Time (s)'); ylabel('PRI (usec)');
xticks([0 .05 .1]);
set(gca, 'ylim', [300 900], 'YGrid', 'on','XGrid', 'on');
set(p, 'MarkerFaceColor', get(p,'Color'));
nexttile;
y2 = repmat(linspace(850, 420, 25),1,4);
x2 = linspace(0, 0.1, 100);
p = plot(x2,y2, 'o', 'MarkerSize', 4);
xlabel('Time (s)'); ylabel('PRI (usec)');
xticks([0 .05 .1]);
set(gca, 'ylim', [300 900], 'YGrid', 'on', 'XGrid', 'on');
set(p, 'MarkerFaceColor', get(p,'Color'));
  2 个评论
Stephen john
Stephen john 2022-3-17
@Scott MacKenzie Thanks for you answer, I want my x axis has 1000 values.
and also can this shape randomly generated in the above code you just putt (850 - 410) I want it to be random. for example some time it between 210 to 500 and some time it between 350 to 600
Stephen john
Stephen john 2022-3-17
编辑:Stephen john 2022-3-17
@Scott MacKenzie you repat 4 times but i want 1000 samples in my array how can i do that?

请先登录,再进行评论。


Simon Chan
Simon Chan 2022-3-17
Try the following and you may adjust the numLines to 1000 (Following demo is using 100).
Nx = 1000;
Ny = 1000;
numLines = 100; % Number of pattern (line) you required
lengthLine = 400; % Length of each pattern (line)
numPt = 20; % Number of points for each pattern (line)
angle = 60; % Angle of pattern (line)
s= 0;
f = figure;
ax = gca;
while s<numLines
xy1 = rand(1,2);
x1 = Nx * xy1(1);
y1 = Ny * xy1(2);
x2 = x1 + lengthLine * cosd(angle);
y2 = y1 + lengthLine * sind(angle);
if all([y1 y2] <= Ny) && all([x1 x2] <= Nx) && all([y1 y2] >= 1) && all([x1 x2] >= 1)
s = s+1;
xx = linspace(x1,x2,numPt);
yy = linspace(y1,y2,numPt);
plot(xx,yy,'o','MarkerSize',3);
hold on;
drawnow
end
end
ax.XLim = [1 Nx];
ax.YLim = [1 Ny];
grid(ax,'on');
  2 个评论
Stephen john
Stephen john 2022-3-17
@Simon Chan it is not my requirement. i want it a simple also the samples are overlapping
Simon Chan
Simon Chan 2022-3-17
May be this? Just don't quite understand about random on x-axis...
Ny = 1000;
numLines = 21;
numPt = 25;
f = figure;
ax = gca;
x1 = 0;
x2 = 1/30;
xdelta = 1/(30*numPt);
for k = 1:numLines
ylength = randi([1 Ny-1]); % Length of each line
ystart = randi([1 Ny-ylength]); % Start position
yend = ystart + ylength; % End position
xx = linspace(x1,x2,numPt);
yy = linspace(ystart,yend,numPt);
plot(xx,yy,'o','MarkerSize',3);
hold on;
x1 = x1+1/30+xdelta;
x2 = x2+1/30;
end
ax.YLim = [0 Ny];
grid(ax,'on');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by