Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Attempted to access DOF_ellipsoid(2,:); index out of bounds because numel(DOF_ellipsoid)=1

Subject: Attempted to access DOF_ellipsoid(2,:); index out of bounds because numel(DOF_ellipsoid)=1

From: Hung Smart

Date: 28 Aug, 2014 02:30:29

Message: 1 of 3

Hello, i'm trying to run the program to find the position of an object using ellipsoids intersection. But got the error above. Here is my code. Please explain to me the reason i got error. DOF_ellipsoid is the distance measured from sensors

h_func = @z_fun_ellipsoid;
dh_dx_func = @Hk_fun_ellipsoid;
H_xx = @Hxx_fun;
DOF = k_est_all.*Sound_vel;
% test of function
z_estimate1 = z_fun_ellipsoid( Measurement_size, x_estimate, T_pos, R_pos, Sound_vel, vs_est, DOF ) ;% ellipsoid model for measurement
Hk = Hk_fun_ellipsoid( Measurement_size, x_estimate, T_pos, R_pos, Sound_vel, vs_est, DOF );
%% Estimate with EKF
[M,P] = ekf_predict2(x_estimate,P,A,Q);% kalman predict process
[x_estimate,P,K,MU,S] = ekf_update2(M,P,observe_TOF,dh_dx_func,H_xx,Ez,h_func, Measurement_size, T_pos, R_pos, Sound_vel, vs_est, DOF);% kalman update process

The first subroutine
function yout = z_fun_ellipsoid( Measurement_size, x_estimate, T_pos, R_pos, Sound_vel, vs_est, DOF )
for i = 1:Measurement_size
        yout (i) = ellipsoid_gen(i,x_estimate(1:2), T_pos, R_pos(i,:), Sound_vel, vs_est,DOF(i) );
end

yout = yout';
end

Ellipsoid subroutine
 function yout = ellipsoid_gen(i, x_estimate, T_pos, R_pos, Sound_vel, vs_est ,DOF_ellipsoid)
ang = acos(( [ T_pos-R_pos(1,:) ]*[ 1 0 ]')/(norm( T_pos-R_pos(1,:) )*norm([ 1 0])));
if R_pos(i)-T_pos(1)>0
    ang = pi-ang;
    ang = -ang;
end
R = [ cos(ang) -sin(ang); sin(ang) cos(ang) ];
% w = [x,y];
alphar = (DOF_ellipsoid(i,:) - norm(R_pos(i,:)))/2;
beta =0.5*sqrt( DOF_ellipsoid(i,:)^2 - norm(R_pos(i,:))^2 );
sigma = [alphar^2 0; 0 beta^2];
P = R'*inv(sigma)*R;
yout = (x_estimate'-0.5*R_pos(i,:))*P*(x_estimate'-0.5*R_pos(i,:))' ;
end

Subject: Attempted to access DOF_ellipsoid(2,:); index out of bounds because numel(DOF_ellipsoid)=1

From: Steven Lord

Date: 28 Aug, 2014 13:32:45

Message: 2 of 3


"Hung Smart" <beastboy_1089@yahoo.com> wrote in message
news:ltm485$h7r$1@newscl01ah.mathworks.com...
> Hello, i'm trying to run the program to find the position of an object
> using ellipsoids intersection. But got the error above. Here is my code.
> Please explain to me the reason i got error. DOF_ellipsoid is the distance
> measured from sensors

*snip*

> The first subroutine
> function yout = z_fun_ellipsoid( Measurement_size, x_estimate, T_pos,
> R_pos, Sound_vel, vs_est, DOF ) for i = 1:Measurement_size
> yout (i) = ellipsoid_gen(i,x_estimate(1:2), T_pos, R_pos(i,:),
> Sound_vel, vs_est,DOF(i) );

You're passing a scalar (a 1-by-1 variable) as the 7th input to
ellipsoid_gen. The first input argument will be an integer value between 1
and Measurement_size. If Measurement_size can be greater than 1, this means
i can be greater than 1.

*snip*

> Ellipsoid subroutine
> function yout = ellipsoid_gen(i, x_estimate, T_pos, R_pos, Sound_vel,
> vs_est ,DOF_ellipsoid)

So in the call to ellipsoid_gen in z_fun_ellipsoid, DOF_ellipsoid is 1-by-1.

> ang = acos(( [ T_pos-R_pos(1,:) ]*[ 1 0 ]')/(norm(
> T_pos-R_pos(1,:) )*norm([ 1 0])));
> if R_pos(i)-T_pos(1)>0
> ang = pi-ang;
> ang = -ang;
> end
> R = [ cos(ang) -sin(ang); sin(ang) cos(ang) ];
> % w = [x,y];
> alphar = (DOF_ellipsoid(i,:) - norm(R_pos(i,:)))/2;

You're using the variable i to index into the rows of DOF_ellipsoid. Based
on what I wrote above, DOF_ellipsoid has exactly 1 row. But i can be a
number greater than 1. That is the cause of the error.

How to solve the problem depends on exactly what you're trying to do. [Some
comments in your code explaining what each step is doing would be useful
when you return to this code in a few days, weeks, months, or years.] You
may need to pass in all of DOF into ellipsoid_gen or you may need to avoid
performing that indexing here.

> beta =0.5*sqrt( DOF_ellipsoid(i,:)^2 - norm(R_pos(i,:))^2 );
> sigma = [alphar^2 0; 0 beta^2];
> P = R'*inv(sigma)*R;

Don't invert sigma! Since sigma is a diagonal matrix, it's easy to calculate
its inverse directly, and it's also easy to identify the conditions under
which sigma is singular (and so HAS no inverse.) [Hint: what is the inverse
of [4 0; 0 10]? What is the relationship between the elements of that matrix
and the elements of its inverse?]

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Attempted to access DOF_ellipsoid(2,:); index out of bounds because numel(DOF_ellipsoid)=1

From: Hung Smart

Date: 31 Aug, 2014 11:43:06

Message: 3 of 3

Thank you for explaining to me enthusiastically
> > The first subroutine
> > function yout = z_fun_ellipsoid( Measurement_size, x_estimate, T_pos, R_pos, Sound_vel, vs_est, DOF ) for i = 1:Measurement_size
for i = 1:Measurement_size
        yout (i) = ellipsoid_gen(i,x_estimate(1:2), T_pos, R_pos(i,:), Sound_vel, vs_est,DOF(i,:) );
end
> I changed the last element to DOF(i,:) but still got these errors. I'm wondering if the problem didn't come from DOF array?

Attempted to access DOF_ellipsoid(2,:); index out of bounds because numel(DOF_ellipsoid)=1.

Error in ellipsoid_gen (line 61)
alphar = (DOF_ellipsoid(i,:) - norm(R_pos(i,:)))/2;

Error in z_fun_ellipsoid (line 3)
     yout (i) = ellipsoid_gen(i,x_estimate(1:2), T_pos, R_pos(i,:), Sound_vel, vs_est,DOF(i,:) );

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us