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?

采纳的回答

Image Analyst
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by