'Attempted to access idx(0); index must be a positive integer or logical' error

1 view (last 30 days)
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?

Answers (2)

Image Analyst
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
Abdulrahman
Abdulrahman on 12 May 2014
Edited: Abdulrahman on 12 May 2014
Basically, what I am trying to do is to define a vector B that has the same value of A except that B start from zero.
In other words, B (i) = 0 if A (i) is not zero until the first zero element comes. Then, B (i) = A (i) for the rest of the vector. where i is the index.
However, it seems to be the way that I am using is not working since the elements are decimal number of fourth digits. I ,also, tried to do as purposed in FAQ:
idx=find(abs(A-0.0000000) < 0.00000001,1);
idx=idx(end);
B=A;
B(1:idx)=0
But still not working. Any idea, how I can achieve the wanted results?
Image Analyst
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.

Sign in to comment.


Star Strider
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
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
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]

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!