For loop that moves through array of X, Y coordinates

조회 수: 5 (최근 30일)
HumbleCoder
HumbleCoder 2023년 2월 14일
댓글: HumbleCoder 2023년 2월 14일
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