Matrix that changes size
Afficher commentaires plus anciens
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
Réponses (2)
David Hill
le 6 Avr 2022
Modifié(e) : David Hill
le 6 Avr 2022
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
le 6 Avr 2022
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
Catégories
En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!