Info

This question is closed. Reopen it to edit or answer.

getting error Subscript indices must either be real positive integers or logicals in finding distance?

1 view (last 30 days)
Hello everyone, let say, i have cell matrix {[100*2],[450*2],[300*2],[999*2]....[898*2]}. Now i want to find a distance/derviative of each cell i.e, [100*2] and others cells. from below code i got the distance values by considering one point distance. but i want to get for each distance points are '3' similarly '5','13'. i am getting an error as Subscript indices must either be real positive integers or logicals.
load('myfile.mat');
for j = 1:length(Profile_number)
xy1 = Profile_number{j,1};
%calculate derivate of 1st element in xy1
d_begin = (xy1(2,2)-xy1(1,2))/(xy1(2,1)-xy1(1,1));
%calculate derivate of last element in xy1
d_end = (xy1(end,2)-xy1(end-1,2))/(xy1(end,1)-xy1(end-1,1));
d_main = [];
for k = 2:length(xy1(1:end,1))-1
d_1 = ((xy1(k+13,2)-xy1(k,2))/(xy1(k+13,1)-xy1(k,1)));
d_2 = ((xy1(k,2)-xy1(k-13,2))/(xy1(k,1)-xy1(k-13,1))); ***** getting error here as highlighted
d_k = 0.5*(d_1 + d_2);
d_main = [d_main;d_k];
end
d_main = [d_begin;d_main;d_end];
profilenumber{j,2} = d_main;
end
Thanks for your help inadvance
  3 Comments
Ram
Ram on 26 Sep 2018
Edited: Ram on 26 Sep 2018
@Adam thanks alot for responding. Please can you tell me how to get reult without this error. I tried with breakpoints/pause error in order to locate error, but for me doesn't work it out and i changed the k indexed from 14 (getting index exceeds matrixdimensions error) and also i tried with 5,9. but i didn't get error with 1.
Adam
Adam on 26 Sep 2018
I'm afraid I don't have the time to try to work out what your algorithm is attempting to do, I was just answering the question at hand which was why you get that error.
I also don't know what you mean by 'doesn't work out'. If you have the option selected then 'Pause on errors' will stop you at the right part of the code to investigate further.

Answers (1)

Walter Roberson
Walter Roberson on 26 Sep 2018
Your k needs to start at least 1 higher than the greatest magnitude that you subtract from your subscript. With you subtracting 13, your k needs to start at least at 14.
Your k needs to end at most at the length of the array minus the greatest magnitude that you add to your subscript. With you adding 13, your k needs to end at length(xy1(1:end,1))-13 or before.
Note: length(xy1(1:end,1)) can be better written as size(xy1, 1)
  2 Comments
Ram
Ram on 26 Sep 2018
@walter Roberson Thanks alot. after i change my length(end) to 13. i got the output without error. You saved my time. Thanks Genius.
Walter Roberson
Walter Roberson on 19 Nov 2018
I notice that you marked that "Another Answer is better", but that there is no other answer. Are you continuing to have difficulty with the Question you had posted?

Community Treasure Hunt

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

Start Hunting!