필터 지우기
필터 지우기

find function and vectors gives Empty matrix: 1-by-0

조회 수: 1 (최근 30일)
Amit Aryal
Amit Aryal 2015년 3월 10일
댓글: Stephen23 2015년 3월 11일
close all
clear variables
clc
w = [-65:0.1:10];%Error membership x axis range
Negbig_error = trapmf(w,[-65 -65 -62 -10]);
ant1 = Negbig_error(find(w == -25.8))
w ranges from -65 to 10 in my code with step of 0.1 but matlab cannot find y axis intercept when w = -25.8
returns error code: Empty matrix: 1-by-0

답변 (1개)

Michael Haderlein
Michael Haderlein 2015년 3월 10일
That's a floating point arithmetics problem. 0.1 cannot be represented exactly. Creating the array your way, you get something like
>> -65+0.1*392
ans =
-25.8000
but that's not exactly -25.8:
>> (-65+0.1*392)+25.8
ans =
3.5527e-15
If I remember correctly, Matlab actually creates such arrays a bit more intelligently than sketched here, but the general issue remains. If you want to search for w==-25.8, you have to search for something close to this value, such as
>> w=-65:.1:10;
>> Negbig_error = trapmf(w,[-65 -65 -62 -10]);
>> w(abs(w+25.8)<.01)
ans =
-25.8000
>> Negbig_error(abs(w+25.8)<.01)
ans =
0.3038
find() is not necessary here, so I skipped it.
  댓글 수: 5
Michael Haderlein
Michael Haderlein 2015년 3월 11일
It's just logical indexing. If you have an array, say [10 20 30 40 50], you can pick elements either by their position number (myarray(2) will return 20 and myarray([2 4]) will return [20 40]) or you can pick them with logicals: myarray([false true false true false]) will also return [20 40]. That's a convenient way to select elements based on their value, e.g. myarray<30 will be [true true false false false]. Use this as logical index and you'll get the values of all elements fulfilling the condition.
Stephen23
Stephen23 2015년 3월 11일
@Amit Aryal: logical indexing is much faster than using find, and can be calcualted directly in many situations, e.g. to remove all negative values from a vector:
A = [1,-3,2,4,-9,];
A(A<0) = []
There are several different indexing methods in MATLAB, and they are each useful in different situations. You can learn about them here:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by