Matrix that changes size
이전 댓글 표시
I need to create a matrix that changes sizes. The user inputs x and y coordinates and then I want to display them as a matrix, but the number of coordinates changes. This is the code i have so far, where NJ=number of x and y coordinates the user inputs and then they input the coordinates:
disp('Part I: Input Joint Data');
NJ=input('Enter Number of Joints, NJ = ');
for I=1:NJ
fprintf('\nEnter coordinate of joint %d\n', I);
COORD(I,1)=input('X coordinates = ');
COORD(I,2)=input('Y coordinates = ');
x=COORD(I,1);
y=COORD(I,2);
end
how do i create the matrix? thanks in advance
답변 (2개)
David Hill
2022년 4월 6일
편집: David Hill
2022년 4월 6일
why not enter them all at once?
m=input('input x and y coordiants in a matrix, as an example [1 2;3 4;5 6]: ');
Voss
2022년 4월 6일
One thing you can do is to pre-allocate COORD to be the correct size after NJ is input by the user. (And if this code is used inside a loop, then you won't have the problem of COORD having too many rows when NJ decreases from one iteration of the loop to the next.)
disp('Part I: Input Joint Data');
NJ=input('Enter Number of Joints, NJ = ');
% create a matrix of the correct size, of all zeros:
COORD = zeros(NJ,2);
for I=1:NJ
fprintf('\nEnter coordinate of joint %d\n', I);
COORD(I,1)=input('X coordinates = ');
COORD(I,2)=input('Y coordinates = ');
x=COORD(I,1);
y=COORD(I,2);
end
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!