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!