find cannot read all values in .mat file
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
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
More Answers (2)
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.
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!