MATLAB Answers

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)
Ram
Ram on 26 Sep 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
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
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
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!