我想绘制从北纬55度和东经20度的特定点开始的一些向量,并根据随机角度跟随方向(用randn生成)。 我想过用循环来做这件事但是没有用完:
for i=1:100
a=50+ 20.*randn;
b = [a];
i = i + 1;
route = [20,50] + b *
plot(route, 'color', 'magenta')
hold on
end
»»»route = [20,50] + b *我试过这样,因为对我而言,它看起来像是y = a + bx类型的愚蠢线性方程式...我只是不知道该怎么做用于x ... 这样它只会绘制1条路线,我需要100 ...
(所以我需要在一个图表上从相同的点开始的100个向量,其中唯一的参数变化是方向)
希望有人可以帮助我。有什么想法吗?
ps:我刚刚开始使用matlab。
答案 0 :(得分:2)
1 - 对于带有方向的一行,请尝试以下代码:
%Initial line information
startPoint = [20 50] ;
direction = [4 3] ;
lineLength = 100;
%Initialize line points with zeros
x = zeros(lineLength);
y = zeros(lineLength);
% Update line points
for i = 1 : lineLength
x(i) = startPoint(1) + direction(1) * i;
y(i) = startPoint(2) + direction(2) * i;
end
%Plot the line
plot( y , x ,'r.');
2 - 如果你想让每个点的方向发生变化,
使用以下代码:
%Initial line values
startPoint = [20 50] ;
lineLength = 100;
%create random direction vector
randomMax = 100;
direction = randi(randomMax,lineLength,2);
%Initialize line points with zeros
x = zeros(lineLength);
y = zeros(lineLength);
%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);
% Update line points accumulating on previous point
for i = 2 : lineLength
x(i) = x(i - 1) + direction(i,1) * i;
y(i) = y(i - 1) + direction(i,2) * i;
end
%Plot the line
plot( y , x ,'r.');
3 - 对于具有不同方向的各种线路,请使用以下代码:
%Initial line values
startPoint = [20 50] ;
lineLength = 100;
%create random the 100 directions vector
randomMax = 100;
directions = randi(randomMax,lineLength,2);
%Initialize line points with zeros
x = zeros(lineLength,100);
y = zeros(lineLength,100);
%set first points
x(1) = startPoint(1);
y(1) = startPoint(2);
h3 = figure;
% Update line points accumulating on previous point
for k = 1 : 100
for i = 2 : lineLength
x(i,k) = x(i - 1,k) + directions(k,1) * i;
y(i,k) = y(i - 1,k) + directions(k,2) * i;
end
%hold the figure and plot the k-th line
hold on;
plot( y(: , k) , x(: , k) , 'r.');
end