How to store values from a loop in a matrix
이전 댓글 표시
Im running a simulation in Simscape Multibody that is outputting the velocities in a 199x1 double (called velocity). Im then changin one of the variables 25 times, in a loop and i want to get a big matrix at the end that will output all of the velocites in one place. This is my current code:
%% landing velocity
%initialise matrix
landing_velcity=[];
for i = 1:25
c=9200;
M=300+(100*i);
system=sim ('dropping.slx');
landing_velocity(i)=velocity
end
The error I'm getting is that the left and right sides have different elements.
답변 (1개)
Pre-allocate your loop variable according to the number of loops and the length of the variable being stored. Here's an example.
landing_velocity = nan(25, numel(velocity)); % updated to fix typos
for i = 1:25
. . .
landing_velocity(i,:)=velocity;
end
댓글 수: 2
Image Analyst
2020년 4월 13일
landing_velocity = nan(25, numel(velocity)); % 2 corrections
Adam Danz
2020년 4월 14일
Good catch.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!