Subscript indices must either be real positive integers or logicals.

1 次查看(过去 30 天)
how can i solve "Subscript indices must either be real positive integers or logicals." in this line?
selected_point = selected_ones(round(number/2))

回答(1 个)

Kelly Kearney
Kelly Kearney 2016-6-22
number is probably <0.5, and is therefore rounded to 0 (or a negative number). And as the error message says, indices have to real positive integers; 0 is not a valid index.
  2 个评论
Nina B
Nina B 2016-6-23
编辑:Stephen23 2016-6-23
Thanks for the answer, but this is my code, and I really dont know where should I correct to not to face this error
I = (rgb2gray(imread('C:\Users\Nina\Desktop\BS-Progect\ImDepthPosT1No1')));
% maximum = max(max(I));
% minimum = min(min(I));
I_bw = im2bw(I);
I_edge = edge(I_bw,'Canny');
quit = 0;
for i=1:size(I_bw,1)
for j=1:size(I_bw,2)
if I_bw(i,j)~=1
quit = 1;
break
end
end
if quit==1;
break
end
end
x01=i;
y01=j;
quit = 0;
for j=1:size(I_bw,2)
for i=1:size(I_bw,1)
if I_bw(i,j)~=1
quit = 1;
break
end
end
if quit==1;
break
end
end
x02=i;
y02=j;
Thin_edge = bwmorph(I_edge,'skel',Inf);
selecteed_region_0 = Thin_edge(min(x01,x02):max(x01,x02),min(y01,y02):max(y01,y02));
selected_ones = find(selecteed_region_0 == 1);
number = size(find(selecteed_region_0 == 1),1);
selected_point = selected_ones(round(number/2));
seleced_cordination = 0;
quit = 0;
for j = min(y01,y02) : max(y01,y02)
for i = min(x01,x02) : max(x01,x02)
seleced_cordination = seleced_cordination + 1;
if seleced_cordination == selected_point;
quit = 1;
break
end
end
if quit == 1;
break
end
end
x0 = i ;
y0 = j ;
fprintf('Our first point is in this cordination --> row = %d ; column =%d\n ',x0,y0);
quit = 0;
for i=1:size(I_bw,1)
for j=size(I_bw,2):-1:1
if I_bw(i,j)~=1
quit = 1;
break
end
end
if quit==1;
break
end
end
x11=i;
y11=j;
quit = 0;
for j=size(I_bw,2):-1:1
for i=1:size(I_bw,1)
if I_bw(i,j)~=1
quit = 1;
break
end
end
if quit==1;
break
end
end
x12=i;
y12=j;
selecteed_region_1 = Thin_edge(min(x11,x12):max(x11,x12),min(y11,y12):max(y11,y12));
% selecteed_region_1 = Thin_edge(1:max(x11,x12)); min(y11,y12):max(y11,y12);
selected_ones_1 = find(selecteed_region_1 == 1);
number_1 = size(find(selecteed_region_1 == 1),1);
selected_point_1 = selected_ones_1(round((number_1)/2));
seleced_cordination_1 = 0;
quit = 0;
for j = min(y11,y12):max(y11,y12)
for i = min(x11,x12):max(x11,x12)
seleced_cordination_1 = seleced_cordination_1 + 1;
if seleced_cordination_1 == selected_point_1;
quit = 1;
break
end
end
if quit == 1;
break
end
end
x1 = i ;
y1 = j ;
% x1 = round((x11+x12)/2);
% y1 = round((y11+y12)/2);
%
% % while(I_bw(x1,y1)==0)
% % y1=y1+1;
% % end
% % y1 = y1 -1;
% dist2 = sqrt((x1 - 1)^2 + (y1 - 329)^2);
% for i = 1 : x1
% for j = 329:-1 : y1
% distance = sqrt((x1 - i)^2 + (y1 - j)^2);
% if distance<dist2 && I_edge(i,j)== 1
% x1 = i;
% y1 = j;
% end
% end
% end
Stephen23
Stephen23 2016-6-23
编辑:Stephen23 2016-6-23
@Nina B: if you want to fix your code your have to understand what your code is doing. You know what cause the error (a non-integer value (possibly zero) used as an index), and you know what line it happens on. Now you have to figure out why this happens. This is called debugging.
Maybe there is a mistake on another line, or maybe the algorithm has not been correctly implemented, or maybe the algorithm is being used outside of its applicable domain, or maybe that algorithm just is not correct for some cases ... there are lots of reasons why bugs occurs, but humans fix them by figuring out why they occur. Not just guessing, but checking all of the values: where is number defined? What values can it possible have? (this is not the same as asking "what values do I want it to have!). Are all of these values acceptable, or will some of them lead to zero indices ? How does the algorithm manage that: should it throw an error, use a default value, or do something else?
These are things that the author of the algorithm has to known and understand.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by