'Attempted to access idx(0); index must be a positive integer or logical' error
3 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Image Analyst
on 12 May 2014
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 Comments
Image Analyst
on 12 May 2014
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
on 12 May 2014
Edited: Star Strider
on 12 May 2014
This works:
A = -10:10;
B = A;
B(B<=0) = 0;
2 Comments
Star Strider
on 12 May 2014
Edited: Star Strider
on 12 May 2014
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
on 12 May 2014
Edited: Image Analyst
on 12 May 2014
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]
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!