Error found: Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in MyCost (line 25) z=z+w(i,j)*d(p(i),p(j)); Error

1 view (last 30 days)
Shivankur Thapliyal
Shivankur Thapliyal on 5 Jan 2022
Answered: Image Analyst on 5 Jan 2022
function z=MyCost(p,model)
for i=1:n-1
for j=i+1:n
  1 Comment
Walter Roberson
Walter Roberson on 5 Jan 2022
We do not know what data type p is. It could be a function, so p(i) an p(j) could be almost anything.
We do not know what data type model.d is.
But... if p is a vector and it does not happen to be positive integers, and if model.d is an array, then d(p(i), p(j)) could easily be an attempt to index d at something that is not an integer.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 5 Jan 2022
See the FAQ for a thorough explanation.
You'll find out that your p are either zero, negative, or some fractional number, not integers.
p(i) and p(j) need to be integers and they aren't.
They are the row and column index of d so they must be integers like 1,2,3,4, etc. Not -37, not 0, not 3.1415. There is no -37th row of any matrix.
Maybe you can just round them, but who knows -- you'll have to figure that out. Why did you expect them to be integers greater than 1? But for some reason, they are not that.


Community Treasure Hunt

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

Start Hunting!

Translated by