Index exceed matrix dimension error

1 view (last 30 days)
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;

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2015
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.

More Answers (1)

Geoff Hayes
Geoff Hayes on 27 Nov 2015
Leticia - the error message Index exceeds matrix dimensions is telling you that the code is trying to access an element within a matrix using an index that exceeds the dimensions of that matrix. So put a breakpoint at line 17 (or your code)
m_flow(:,i)= v_flow(:,i)*de;
and call your function. When the debugger pauses at this line look at the value of i and the dimensions for m_flow and v_flow. Does accessing the ith column of v_flow make sense?
  2 Comments
Leticia Campello
Leticia Campello on 27 Nov 2015
So, should I create another function before writing m_flow(:,i)= v_flow(:,i)*de; ? I am really stuck with this because this afternoon was working and now it is not working anymore!!
Geoff Hayes
Geoff Hayes on 27 Nov 2015
Leticia - when you step through the code, what are the dimensions of m_flow and v_flow? What is the i that is causing the problem? I don't have access to your data so cannot comment on why you are observing this error or whether another function is needed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!