'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?
Afficher commentaires plus anciens
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
VBBV
le 3 Mai 2023
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
Alperen Köse
le 3 Mai 2023
VBBV
le 3 Mai 2023
in the m-file that calls the simulink model you posted
Alperen Köse
le 3 Mai 2023
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 )
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
Alperen Köse
le 3 Mai 2023
Alperen Köse
le 3 Mai 2023
Mathieu NOE
le 3 Mai 2023
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)
Alperen Köse
le 3 Mai 2023
Alperen Köse
le 3 Mai 2023
Mathieu NOE
le 3 Mai 2023
trying to do my official work too ...
Alperen Köse
le 3 Mai 2023
Catégories
En savoir plus sur Subsystems dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
