How to generate Pattern in MATLAB
17 ビュー (過去 30 日間)
古いコメントを表示
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 .
0 件のコメント
回答 (3 件)
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 件のコメント
Jon
2022 年 3 月 17 日
Replace the first line in the code above with the following
x = linspace(0,0.1,1000)
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 件のコメント
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 件のコメント
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 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!