Index exceeds matrix dimension in a for loop?

조회 수: 2 (최근 30일)
lauuser1
lauuser1 2016년 3월 3일
답변: Stalin Samuel 2016년 3월 3일
function[pos_N] = func(v_0, windspeed)
pos_N(1,1) = 0; %initial distance
pos_N(1,2) = 500; %initial elevation
%loop starts here
v = v_0;
theta = 20;
g = 9.81;
for t=0:0.1:1000
horiz_velocity = v*cos(theta)+windspeed;
vert_velocity = v_0*sin(theta)+g*sin(theta)*t;
row_number = 1+10*t;
if t==0
else
pos_N_(row_number,1) = pos_N((row_number-1),1)+horiz_velocity*0.1; %position horizontal
pos_N_(row_number,2) = pos_N((row_number-1),2)-vert_velocity*0.1; %elevation decreases because he descends down
... and there is more to the code but at those last two lines, the code has the error "index exceeds matrix dimension".
Why? I have not defined any set size for the matrix pos_N.

답변 (1개)

Stalin Samuel
Stalin Samuel 2016년 3월 3일
function[pos_N] = func(v_0, windspeed)
pos_N = zeros(max(1+10*(0:0.1:1000)),2);%matrix initialization
pos_N(1,1) = 0; %initial distance
pos_N(1,2) = 500; %initial elevation
%loop starts here
v = v_0;
theta = 20;
g = 9.81;
for t=0:0.1:1000
horiz_velocity = v*cos(theta)+windspeed;
vert_velocity = v_0*sin(theta)+g*sin(theta)*t;
row_number = round(1+10*t);
if t==0
else
pos_N_(row_number,1) = pos_N((row_number-1),1)+horiz_velocity*0.1; %position horizontal
pos_N_(row_number,2) = pos_N((row_number-1),2)-vert_velocity*0.1; %elevation decreases because he descends down

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by