'Index exceeds array dimensions. Index value 0 exceeds valid range [1-12] for array 'A'.' I get this error when I run the code? Can you please help?

function cx_value = cx( alpha, el )
A = [ -0.099 -0.081 -0.081 -0.063 -0.025 0.044 0.097 0.113 0.145 0.167 0.174 0.166;
-0.048 -0.038 -0.040 -0.021 0.016 0.083 0.127 0.137 0.162 0.177 0.179 0.167;
-0.022 -0.020 -0.021 -0.004 0.032 0.094 0.128 0.130 0.154 0.161 0.155 0.138;
-0.040 -0.038 -0.039 -0.025 0.006 0.062 0.087 0.085 0.100 0.110 0.104 0.091;
-0.083 -0.073 -0.076 -0.072 -0.046 0.012 0.024 0.025 0.043 0.053 0.047 0.040];
A = A';
row =3 ; %3
col =3; %3
s = 0.2 * alpha;
k = fix ( s );
if ( k <= -2 )
k = -1;
end
if ( k >= 9 )
k = 8;
end
da = s - k;
l = k + fix ( sign ( da ) * 1.1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% added as boundary condition
if l < -2
l = -2;
elseif l > 9
l = 9;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = el / 12.0;
m = fix ( s );
if ( m <= -2 )
m = -1;
end
if ( m >= 2)
m = 1;
end
de = s - m;
n = m + fix ( sign ( de ) * 1.1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% added as boundary condition
if n < -2
n = -2;
elseif n > 2
n = 2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t = A( nansum(k+row), nansum(m+col) ); %numsum ı ben ekledim.
u = A( nansum(k+row), nansum(n+col) );
v = t + abs( da ) * ( A( l+row, m+col ) - t );
w = u + abs( da ) * ( A( l+row, n+col ) - u );
cx_value = v + ( w - v ) * abs( de );

4 commentaires

if you pass alpha & el as vectors , then function errs due to dimension inconsistency.
You might want to avoid such errors by calling function within a loop as below
for k = 1: length(alpha)
cx_value(k) = cx( alpha(k), el(k) );
end
% do something next
Where do I add this in the code?
in the m-file that calls the simulink model you posted

Connectez-vous pour commenter.

Réponses (1)

hello
I replaced nansum which is outdatted by sum with omitnan parameter
seems to work, at least for the input data I picked (you did not provide any test data)
% test function with data ??
cx_value = cx( 10, 10 )
cx_value = 0.0103
function cx_value = cx( alpha, el )
A = [ -0.099 -0.081 -0.081 -0.063 -0.025 0.044 0.097 0.113 0.145 0.167 0.174 0.166;
-0.048 -0.038 -0.040 -0.021 0.016 0.083 0.127 0.137 0.162 0.177 0.179 0.167;
-0.022 -0.020 -0.021 -0.004 0.032 0.094 0.128 0.130 0.154 0.161 0.155 0.138;
-0.040 -0.038 -0.039 -0.025 0.006 0.062 0.087 0.085 0.100 0.110 0.104 0.091;
-0.083 -0.073 -0.076 -0.072 -0.046 0.012 0.024 0.025 0.043 0.053 0.047 0.040];
A = A';
row =3 ; %3
col =3; %3
s = 0.2 * alpha;
k = fix ( s );
if ( k <= -2 )
k = -1;
end
if ( k >= 9 )
k = 8;
end
da = s - k;
l = k + fix ( sign ( da ) * 1.1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% added as boundary condition
if l < -2
l = -2;
elseif l > 9
l = 9;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = el / 12.0;
m = fix ( s );
if ( m <= -2 )
m = -1;
end
if ( m >= 2)
m = 1;
end
de = s - m;
n = m + fix ( sign ( de ) * 1.1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% added as boundary condition
if n < -2
n = -2;
elseif n > 2
n = 2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% t = A( nansum(k+row), nansum(m+col) ); %numsum ı ben ekledim.
% u = A( nansum(k+row), nansum(n+col) );
t = A( sum(k+row,"omitnan"), sum(m+col,"omitnan") ); %numsum ı ben ekledim.
u = A( sum(k+row,"omitnan"), sum(n+col,"omitnan") );
v = t + abs( da ) * ( A( l+row, m+col ) - t );
w = u + abs( da ) * ( A( l+row, n+col ) - u );
cx_value = v + ( w - v ) * abs( de );
end

7 commentaires

I'm trying to extract the F16 model. The cx_value changes with the 'alpha' and 'el' entries. But it gives error. If you want to take a closer look, I can send you the whole model.
so that m code is inside a simulink block ... and what are the dimensions of alpha and elv in your simulink file ?
could you test the m code block alone in a separate simplified simulink file ?
if you want me to test your simulink file I need it in R2020b format (yes I know I'm not super up to date)
I have attached the model, thank you for your interest.
trying to do my official work too ...
Sorry, I was hasty. Have a nice day.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by