0

我想从北 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 条......

(所以我需要在一张图上从同一个点开始的一百个向量,其中唯一变化的参数是方向)

希望可以有人帮帮我。有任何想法吗?

ps:我刚开始matlab。

4

1 回答 1

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
于 2012-12-29T16:16:33.770 回答