'Attempted to access idx(0); index must be a positive integer or logical' error
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have a large vector of size:
size( A )
ans =
612245 1
The elements in the vector has decimal number up to the fourth digits like: -1.9084
I want to find the first zero element and store the index varible idx
I tried to do it as the following but it give me an error:
zero_position= 1;
idx=find(A == 0 ,zero_position);
The following error is show up:
Attempted to access idx(0); index must be a positive integer or logical.
This is also happen when I chose non zero element like:
idx=find(A == -1.9084,1);
How I can solve the problem?
댓글 수: 0
답변 (2개)
Image Analyst
2014년 5월 12일
The error did not come from those lines. It came when you tried to use idx.
Also, you can't compare floating point numbers like that. Please see the FAQ for the proper way to do it using a tolerance: http://matlab.wikia.com/wiki/FAQ?title=FAQ&cb=3385#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
댓글 수: 2
Image Analyst
2014년 5월 12일
Try this:
firstZero = find(A == 0, 1, 'first'); % Index of first place A=0
B = A; % Initialize
B(1:firstZero) = 0 % Zero until A=0, then equals A afterwards.
Star Strider
2014년 5월 12일
편집: Star Strider
2014년 5월 12일
This works:
A = -10:10;
B = A;
B(B<=0) = 0;
댓글 수: 2
Star Strider
2014년 5월 12일
편집: Star Strider
2014년 5월 12일
I got the opposite impression when I read your question. Simply switch the test:
A=[1.9734 1.9577 1.6778 1.1715 0.0000 -0.2257 -0.9281 -1.5052]
B = A;
B(B>=0) = 0;
produces:
B = 0.0000 0.0000 0.0000 0.0000 0.0000 -0.2257000 -0.9281000 -1.5052
Image Analyst
2014년 5월 12일
편집: Image Analyst
2014년 5월 12일
I think Abdulrahman's "Answer" is directed towards Start Strider so I'm moving it here:
No, it does not work :(
1st, The vector elements are decimal numbers of fourth digits like -0.1957
2nd, Vector B should be zero before the first zero element in A comes. Then, B equal A after first zero element in A.
For example:
A=[1.9734 1.9577 1.6778 1.1715 0.0000 -0.2257 -0.9281 -1.5052]
So,
B= [0.0000 0.0000 0.0000 0.0000 0.0000 -0.2257 -0.9281 -1.5052]
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!