Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 view (last 30 days)
Good evening,
while I run this program, it displays:
"Index in position 1 is invalid. Array indices must be positive integers or logical values."
Error in max_riga (line 3)
max=Mat(i,j);
Error in main (line 5)
MAX=max_riga(N,A);
This is the main program.
clc;clear;
N=3;
A=carica_mat(N);
disp(A)
for i=1:N
MAX=max_riga(N,A,i);
end
ord_bolle_vet(N,MAX)
MIN=minimo(N,MAX);
disp(MIN)
And this is "max_riga" function
function[max]=max_riga(s,Mat,x)
j=1;
max=Mat(x,j);
for j=2:s
if Mat(x,j)> max
max=Mat(x,j);
end
end
end
What can I do to fix this problem?
  6 Comments
Bob Thompson
Bob Thompson on 11 Feb 2019
I don't expect this to solve the problem, but try changing the name of the 'max' variable. I suggest this because max is a built-in matlab function, and may be causing you problems.
The original error gives indication that the 'i' value being fed into that line is either negative or not an integer. I would suggest placing a debug marker on that line so you can check the value of 'i' before it actually enters the command.
The second error seems to indicate that something changed in the definition of the max_riga function. I would suggest double checking the calling line in the main program, and the function definition line in the function.
If none of those things yield any results, then it would be helpful if you could also post the other functions and sample inputs to be able to run the script on our own, that way we can trouble shoot directly.
Geoff Hayes
Geoff Hayes on 11 Feb 2019
Carlo - the error message is showing
Error in main (line 5)
MAX=max_riga(N,A);
where you are not passing in a third parameter...yet the function signature is expecting one. Is this error message still valid since your code is now
for i=1:N
MAX=max_riga(N,A,i);
end
where you are passing in the third parameter which is an integer between 1 and 3? Are you perhaps seeing a different error message?

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!