Matrix that changes size
Ältere Kommentare anzeigen
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
Antworten (2)
David Hill
am 6 Apr. 2022
Bearbeitet: David Hill
am 6 Apr. 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
am 6 Apr. 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
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!