MATLAB: Index exceed matrix dimension error

functionmatrix

Everytime time that I run my code, this error ocurrs:
Index exceeds matrix dimensions.
Error in analyse_lbbc (line 17)
m_flow(:,i)= v_flow(:,i)*de;
what should I do ? Thank You
function [v_flow,m_flow,v1,F_1,F_2,F_1_mean,F_2_mean]= analyse_lbbc(m,v,t);
%create some variable
m=[0.4 0.3 0.2 0.1];
v=[0.003;0.003;0.003;0.003];
t=[11.3 11.8 16.8 20.4];
%save the variable m,v,t into one *.mat file
save data.mat m v t
%load the data
load data.mat
d= 0.005; %diameter
de=0.001; %density of the water
g=9.81; %gravity
A=(pi/4)*(d^2); %cross section area of the nozzle
c=0
for i=1:4
v_flow(i,:)= v(i,:)./t(:,i);
m_flow(:,i)= v_flow(:,i)*de;
v1=v_flow(:,i)*A;
F_1= m(:,i)*g;
F_2= m_flow(:,i).*v1(:,i);
end
F_1_mean = mean(:,1:4);
F_2_mean= mean(:,1:4);
c=F_1_mean - m(:,1:4)*F_2_mean;

Best Answer

Look more closely at your code. You have
v_flow(i,:)= v(i,:)./t(:,i);
m_flow(:,i)= v_flow(:,i)*de;
In the first line you write into the i'th row of v_flow, but in the second line you read from the i'th column of v_flow. If your v_flow does not happen to contain at least 4 columns then reading from the i'th column would be index exceeding matrix dimensions.