如何在matlab中以度为单位从起点绘制向量并沿特定方向绘制?

如何在matlab中以度为单位从起点绘制向量并沿特定方向绘制?,matlab,Matlab,我想画一些向量,从一个特定的点开始,向北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*我这样试过,因为对我来说,它

我想画一些向量,从一个特定的点开始,向北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用什么。。。 同样,这样它将只绘制一条路线,我需要100

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

希望有人能帮助我。有什么想法吗


ps:我刚刚开始使用matlab。

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.');
%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
3-对于不同方向的不同线路,使用以下代码:

%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.');
%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

谢谢你的第一部分。但对于第二部分,我想改变100次的不是直线的方向,而是整个向量的方向。我想在同一个图上有100个向量,有100个不同的随机方向。。。但线宽不变。长度不重要。。。这就是我下一步的问题!尝试答案的新第三部分,每一行都有不同的方向。