'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?

回答(2 个)

Image Analyst
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 个评论
Abdulrahman
Abdulrahman 2014-5-12
编辑:Abdulrahman 2014-5-12
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 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
Star Strider 2014-5-12
编辑:Star Strider 2014-5-12
This works:
A = -10:10;
B = A;
B(B<=0) = 0;
  2 个评论
Star Strider
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
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 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