필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Abdulrahman
Abdulrahman 2014년 5월 12일
편집: Star Strider 2014년 5월 12일
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