0
votes

i have the following error in my matlab function code:

??? Subscript indices must either be real positive integers or logicals.

Error in ==> AFA at 15 M(k,j) = mean(T(i:sze,j));

here is the part of the code where the problem is :

sz =size(T);
lim = sz(2) - ordre;
M = zeros(sz(1),sz(2));
r= 0;
for j=1:sze,
    k = 1;
for i=1:lim,
    M(k,j) = mean(T(i:i+ordre,j));
    k = k + 1;    
end

for i=lim+1:sz(2),
    M(k,j) = mean(T(i:sz(2),j));
    k = k + 1; 
end
end
1
Sounds like ordre is zero, then lim+1 > sz(2). Just a guess... - Diego
no, actually ordre=5 at this point - Tarik Mokafih
Can matlab print data to the screen or a log file? Could you have it print the state of all variables before that function call, and then review the log to see what index it's complaining about? - tomlogic

1 Answers

0
votes

The following works for me without error

T = magic(25);ordre = 5; %# I make up some values here
sz =size(T);
lim = sz(2) - ordre;
%# I've added a check here
if lim < 1, error('ordre has to be at most sz(2)-1 (is %i)',ordre);end
M = zeros(sz(1),sz(2));
r= 0;
for j=1:sz(2), %# I needed to change this line (sz(1) works as well)
    k = 1;
for i=1:lim,
    M(k,j) = mean(T(i:i+ordre,j));
    k = k + 1;    
end

for i=lim+1:sz(2),
    M(k,j) = mean(T(i:sz(2),j));
    k = k + 1; 
end

end