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

3 views (last 30 days)
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

Answers (1)

Michael Haderlein
Michael Haderlein on 10 Mar 2015
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 Comments
Michael Haderlein
Michael Haderlein on 11 Mar 2015
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 on 11 Mar 2015
@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:

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!