For loop that moves through array of X, Y coordinates
6 views (last 30 days)
Show older comments
HumbleCoder
on 14 Feb 2023
Commented: HumbleCoder
on 14 Feb 2023
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 Comments
Accepted Answer
Image Analyst
on 14 Feb 2023
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!