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!