From: James on 26 Apr 2010 14:52 "the cyclist" <thecyclist(a)gmail.com> wrote in message <hr4goo$gbu$1(a)fred.mathworks.com>... > "James " <cche5398(a)uni.sydney.edu.au> wrote in message <hr4f80$6i0$1(a)fred.mathworks.com>... > > I have a 361 x 2 matrix. Below is just a portion of the data taken from my file. > > The left side refers to degrees and the right side is the distance from the center. > > 167 161.423179453702 > > 168 161.032351564031 > > 169 160.821945416392 > > 170 160.793632931030 > > 171 160.945732200972 > > 172 161.208529486586 > > 173 161.780262247064 > > 174 162.522266671831 > > 175 163.536760034243 > > 176 164.411165803307 > > 177 165.398412869153 > > 178 166.293554332660 > > 179 167.673046265240 > > > > How do i pair the left and right side up so that whenever i enter degrees it would actually give me the corresponding distance? > > For example, if i type deg(167) in the command window, i am hoping to get ans = 161.423179453702, or if i enter deg(178), i want it to give me ans = 166.293554332660 > > The exact syntax will depend on whether your array is stored in a file, in workspace, etc. However, if "A" is your array, then the operative commands will be something like: > > >> indexToCorrectRow = (A(:,1)==167); > >> value = A(indexToCorrectRow,2); > > The first line finds the row of A that has 167 in the first column. The second line finds the element in the second column of that row. > > the cyclist Thanks, mate. But as Steven Lord said, the above code may not work 100% in some of the situation he just mentioned. So i think i will stick to using INTERP1.
From: the cyclist on 26 Apr 2010 15:08 "James " <cche5398(a)uni.sydney.edu.au> wrote in message <hr4n1d$dn6$1(a)fred.mathworks.com>... > "Steven Lord" <slord(a)mathworks.com> wrote in message <hr4h19$4im$1(a)fred.mathworks.com>... > > > > "the cyclist" <thecyclist(a)gmail.com> wrote in message > > news:hr4goo$gbu$1(a)fred.mathworks.com... > > > "James " <cche5398(a)uni.sydney.edu.au> wrote in message > > > <hr4f80$6i0$1(a)fred.mathworks.com>... > > >> I have a 361 x 2 matrix. Below is just a portion of the data taken from > > >> my file. > > >> The left side refers to degrees and the right side is the distance from > > >> the center. > > >> 167 161.423179453702 > > >> 168 161.032351564031 > > >> 169 160.821945416392 > > >> 170 160.793632931030 > > >> 171 160.945732200972 > > >> 172 161.208529486586 > > >> 173 161.780262247064 > > >> 174 162.522266671831 > > >> 175 163.536760034243 > > >> 176 164.411165803307 > > >> 177 165.398412869153 > > >> 178 166.293554332660 > > >> 179 167.673046265240 > > >> > > >> How do i pair the left and right side up so that whenever i enter degrees > > >> it would actually give me the corresponding distance? > > >> For example, if i type deg(167) in the command window, i am hoping to get > > >> ans = 161.423179453702, or if i enter deg(178), i want it to give me ans > > >> = 166.293554332660 > > > > > > The exact syntax will depend on whether your array is stored in a file, in > > > workspace, etc. However, if "A" is your array, then the operative > > > commands will be something like: > > > > > >>> indexToCorrectRow = (A(:,1)==167); > > >>> value = A(indexToCorrectRow,2); > > > > > > The first line finds the row of A that has 167 in the first column. The > > > second line finds the element in the second column of that row. > > > > The code the cyclist wrote will work if all the degree values you're > > planning to use are in your data, but could run into issues if you ask for > > the distance corresponding to an angle that's not in your data or is > > slightly different from what is in your data. If your usage could fall into > > that use case, I suggest using INTERP1. > > > > -- > > Steve Lord > > slord(a)mathworks.com > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > > > Yep, INTERP1, that's what i tried initially. But i couldn't get that to work, i did look through the help before posting this thread, > > YI = INTERP1(X,Y,XI) interpolates to find YI, the values of the underlying function Y at the points in the array XI. X must be a vector of length N. > > I am having trouble understanding what XI is. In the above case, X is degrees and Y is distance from center. What's XI? X is the first column of your array. Y is the second column of your array. XI is the input to your function (the "degrees" value that your user is interested in). YI is the output of your function (the "distance" that your users wants to know). the cyclist
From: Sean on 26 Apr 2010 15:21 "James " <cche5398(a)uni.sydney.edu.au> wrote in message <hr4n1d$dn6$1(a)fred.mathworks.com>... > "Steven Lord" <slord(a)mathworks.com> wrote in message <hr4h19$4im$1(a)fred.mathworks.com>... > > > > "the cyclist" <thecyclist(a)gmail.com> wrote in message > > news:hr4goo$gbu$1(a)fred.mathworks.com... > > > "James " <cche5398(a)uni.sydney.edu.au> wrote in message > > > <hr4f80$6i0$1(a)fred.mathworks.com>... > > >> I have a 361 x 2 matrix. Below is just a portion of the data taken from > > >> my file. > > >> The left side refers to degrees and the right side is the distance from > > >> the center. > > >> 167 161.423179453702 > > >> 168 161.032351564031 > > >> 169 160.821945416392 > > >> 170 160.793632931030 > > >> 171 160.945732200972 > > >> 172 161.208529486586 > > >> 173 161.780262247064 > > >> 174 162.522266671831 > > >> 175 163.536760034243 > > >> 176 164.411165803307 > > >> 177 165.398412869153 > > >> 178 166.293554332660 > > >> 179 167.673046265240 > > >> > > >> How do i pair the left and right side up so that whenever i enter degrees > > >> it would actually give me the corresponding distance? > > >> For example, if i type deg(167) in the command window, i am hoping to get > > >> ans = 161.423179453702, or if i enter deg(178), i want it to give me ans > > >> = 166.293554332660 > > > > > > The exact syntax will depend on whether your array is stored in a file, in > > > workspace, etc. However, if "A" is your array, then the operative > > > commands will be something like: > > > > > >>> indexToCorrectRow = (A(:,1)==167); > > >>> value = A(indexToCorrectRow,2); > > > > > > The first line finds the row of A that has 167 in the first column. The > > > second line finds the element in the second column of that row. > > > > The code the cyclist wrote will work if all the degree values you're > > planning to use are in your data, but could run into issues if you ask for > > the distance corresponding to an angle that's not in your data or is > > slightly different from what is in your data. If your usage could fall into > > that use case, I suggest using INTERP1. > > > > -- > > Steve Lord > > slord(a)mathworks.com > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > > > Yep, INTERP1, that's what i tried initially. But i couldn't get that to work, i did look through the help before posting this thread, > > YI = INTERP1(X,Y,XI) interpolates to find YI, the values of the underlying function Y at the points in the array XI. X must be a vector of length N. > > I am having trouble understanding what XI is. In the above case, X is degrees and Y is distance from center. What's XI? XI is the point you care about e.g: 167 or 167.3 or 89.91 degrees. You have to be careful with your data since it changes quickly over a long period. If you use all of the angles and all of the distances for your x, y then the result will most likely be garbage. You probably just want to use 1-3 values on each side of the angle you'll be interpolating. e.g. say you want it at 167.5, with 1 angle on each side: angles = your_matrix_earlier(:,1); distances = your_matrix_earlier(:,2); my_distance = interp1(angles(167:168),distances(167:168), 167.5);
From: the cyclist on 26 Apr 2010 15:40
"Sean " <sean.dewolski(a)nospamplease.umit.maine.edu> wrote in message <hr4p30$pki$1(a)fred.mathworks.com>... > XI is the point you care about e.g: 167 or 167.3 or 89.91 degrees. > > You have to be careful with your data since it changes quickly over a long period. If you use all of the angles and all of the distances for your x, y then the result will most likely be garbage. You probably just want to use 1-3 values on each side of the angle you'll be interpolating. > > e.g. say you want it at 167.5, with 1 angle on each side: > angles = your_matrix_earlier(:,1); > distances = your_matrix_earlier(:,2); > my_distance = interp1(angles(167:168),distances(167:168), 167.5); The default method, linear interpolation, will ignore everything except the nearest X points. No need to complicate the code in this way. the cyclist |