让我们说我在MATLAB中的点(0,0),我想在坐标平面中移动,让我们用向量[1,1]说。显然,我可以手动将1和1添加到x和y坐标,或者我可以设置 v = [1,1] 并将x增加x(1)和y增加v(2)或类似的规定。但是,让我们说我不想这样做。例如,假设我的目标是绘制由以下算法生成的图形。
我如何直接使用矢量?换句话说,MATLAB中是否存在允许您直接执行 position = current position + vector 之类的操作?谢谢!
答案 0 :(得分:1)
根据我的理解,这是一种方法:
clc
clear
%// The 2 displacement vectors
d1 = [1 1];
d2 = [1 -1];
%// Create a matrix where each row alternates between d1 and d2.
%// In the following for-loop we will access each row one by one to create the displacement.
D = repmat([d2;d1],51,1);
%/ Initialize matrix of positions
CurrPos = zeros(101,2);
%// Calculate and plot
hold all
for k = 2:101
CurrPos(k,:) = CurrPos(k-1,:) + D(k,:); %// What you were asking, I think.
%// Use scatter to plot.
scatter(CurrPos(k,1),CurrPos(k,2),40,'r','filled')
end
box on
grid on
输出:
这是你的想法吗?