Index a number of coordinates as vertex(1), vertex(2), ... vertex(n) by using a for loop and vertex(n) = [x(1,n) y(1,n)]
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to index the vertices as vertex(1) ,vertex(2), vertex(n), but I have not been successful. Can I achieve this in the below for loop? Or do I need a seperate for loop? Currently when run, this section reads the error:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
No_vertices = input('Enter number of vertices: ');
for n = 1:No_vertices
x(1,n) = input('Enter x-coordinate:');
x(n) = x(1,n);
y(1,n) = input('Enter y-coordinate:');
y(n) = y(1,n);
vertex(n) = [x(n) y(n)];
n = n + 1;
end
Here, I enter the number of vertixes in a polygon, the for loop allows me to enter the x and y coordinates for the number of vertices I input. The x and y coordinates are stored and able to be used later as x(1), x(2), .. x(n) and y(1), y(2), ...y(n).
댓글 수: 0
채택된 답변
Aditya Gupta
2020년 6월 24일
편집: Aditya Gupta
2020년 6월 24일
Are you perhaps trying to do something like this?
No_vertices = input('Enter number of vertices: ');
x = zeros(1,No_vertices);
y = zeros(1,No_vertices);
vertex = zeros(2,No_vertices);
for n = 1:No_vertices
x(1,n) = input('Enter x-coordinate:');
y(1,n) = input('Enter y-coordinate:');
vertex(1,n) = x(1,n);
vertex(2,n) = y(1,n);
end
You don't need to increment iterator of the for loop, "for" does that automatically.
Otherwise, I do not understand what you were trying to do.
댓글 수: 0
추가 답변 (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!