For loop that moves through array of X, Y coordinates
11 次查看(过去 30 天)
显示 更早的评论
I am trying to write a program that moves a picture around a screen at designated X,Y coordinates. I have assigned the X and Y vector locations and paired them in an array,
X_vector = [3; -3; -9; -15; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -15; -9; -3; 3;];
Y_vector = [15; 15; 15; 15; 9; 9; 9; 9; 9; 3; 3; 3; 3; 3; -3; -3; -3; -3; -3; -9; -9; -9; -9; -9; -15; -15; -15; -15;];
Paired_Vectors = [X_vector, Y_vector];
but am having trouble telling Matlab to loop through the paired X,Y coordinates to assign the location of the picture to new locations after a specified amount of time.
What type of for loop should I run? Would it be better to run a for loop for X_vector and Y_vector indepedently? How would I still maintain the paired relationships?
0 个评论
采纳的回答
Image Analyst
2023-2-14
Since you already have the x and y separately I don't see why you need to create a new, single matrix out of them. Just use the existing ones you already have.
X_vector = [3; -3; -9; -15; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -21; -15; -9; -3; 3; 3; -3; -9; -15; -21; -15; -9; -3; 3;];
Y_vector = [15; 15; 15; 15; 9; 9; 9; 9; 9; 3; 3; 3; 3; 3; -3; -3; -3; -3; -3; -9; -9; -9; -9; -9; -15; -15; -15; -15;];
for k = 1 : numel(X_vector)
x = X_vector(k);
y = Y_vector(k);
% Now use x and y in some way.
end
The relationship is still "paired" because for any given index, the x value and y value correspond to each other.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!