How to generate Pattern in MATLAB

조회 수: 17 (최근 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