find cannot read all values in .mat file

1 view (last 30 days)
I have a .mat file with about 12000 data, and 6 columns. this file consist of longitude and latitude and the corresponding altitude. My prob is that i use 'find' to get the row of the corresponding altitude from the input of longitude and latitude that I specified. I am sure that the input value i provided is there inside the .mat file. When using find I received the answer for 14 inputs but when the inputs are 34 and 87, find cannot find the corresponding longitude and altitude. It returns the row(r) as empty. Why is that? here is a snipet of my command. lo(longitude) and la (latitude) is the input specified by me.
long=wr1(:,1)/1000; %deg
lat=wr1(:,3)/1000; %deg
alt=wr1(:,6)*0.1; %meter
lo=(linspace(-5.2,8,34))';
la=(linspace(49,42.4,34))';
latlong=[lat,long,alt];
z=[];
for dd=1:34
la2=la(dd);
lo2=lo(dd);
[r,c,v]=find(latlong(:,1)== la2 & latlong(:,2)==lo2);
Z_Alt=alt(r);
z=[z;Z_Alt];
end
For 87 inputs this command returns only 15 outputs for altitude. Is my coding bad? i tried changing to other computers to see if the problem persist, and yes it is still there. I am using Matlab 2012a. thank you.

Accepted Answer

Andreas Goser
Andreas Goser on 17 Dec 2014
My theory is that this is related to numerical effects and e.g. latlong(:,1) and la2 are not exactly equal. Can you analyze the difference between e.g. latlong(:,1) and la2 and see whether the difference is close to zero in the points of interest, but larger than "eps"?
  3 Comments
Andreas Goser
Andreas Goser on 17 Dec 2014
Let me share an example. Assume you have two 1x3 matrices and 1 element is pretty equal while the others not:
format long
A=[1 10 100]
B=[1+2*eps 10+1 100+1]
Now I run the difference:
B-A
ans =
0.000000000000000 1.000000000000000 1.000000000000000
So the first elements are the same as the difference is 0, right? No they are not:
A(1)==B(1)
ans =
0
Mastura
Mastura on 18 Dec 2014
Thanks for telling me about this. I managed to round-off my data to the decimal points I want, and it solved the problem.

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 17 Dec 2014
If you change the number of intermediate values in your linspace function you probably get some values that are not contained in your .dat file.
For example, if your dat file contain longitudes 1, 2, 3, ..., 10 (just for the case of simplicity), you will find corresponding values if you use
lo = linspace(1,10,10)
ans =
1 2 3 4 5 6 7 8 9 10
But for
lo = linspace(1,10,12)
ans =
1.0000 1.8182 2.6364 3.4545 4.2727 5.0909 5.9091 6.7273 7.5455 8.3636 9.1818 10.0000
you will find corresponding rows only for 1 and 10.
  1 Comment
Mastura
Mastura on 17 Dec 2014
I already checked the linspace, and from the result it is like the first example you gave. and I cross check it with the value in the table. It is the same. but maybe it is like what Mr Goser said, that there maybe a small error in the result of the linspace.

Sign in to comment.


Thorsten
Thorsten on 17 Dec 2014
Try
lo=[ -5.2:0.1:8]';
la=[49:-0.1:42.4]';
to get all values at 0.1 spacing.
  1 Comment
Mastura
Mastura on 18 Dec 2014
Thank you for reminding me easier way to write command. it actually helped. Find manage to read 21 data out of 34. But i rounded the decimals and the problem is solved.

Sign in to comment.

Categories

Find more on Language Fundamentals 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!