How to generate Pattern in MATLAB

20 views (last 30 days)
Stephen john
Stephen john on 16 Mar 2022
Commented: Jon on 17 Mar 2022
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 .

Answers (3)

Jon
Jon on 16 Mar 2022
Edited: Jon on 16 Mar 2022
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 Comments
Jon
Jon on 17 Mar 2022
Replace the first line in the code above with the following
x = linspace(0,0.1,1000)

Sign in to comment.


Scott MacKenzie
Scott MacKenzie on 16 Mar 2022
Edited: Scott MacKenzie on 16 Mar 2022
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 Comments
Stephen john
Stephen john on 17 Mar 2022
@Scott MacKenzie you repat 4 times but i want 1000 samples in my array how can i do that?

Sign in to comment.


Simon Chan
Simon Chan on 17 Mar 2022
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 Comments
Simon Chan
Simon Chan on 17 Mar 2022
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');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by