Using the find function in a for-loop: Need to solve an error - "Unable to perform assignment because the left and right sides have a different number of elements"

조회 수: 4 (최근 30일)
Hi!
I'm trying to replace uncorrected GNSS coordinates with their PPK solution coordinate. My code hinges on a for-loop that uses the find function to replace the uncorrected GNSS coordinate with the correct PPK coordinate. The find function is used to identify corresponding time-stamps between the two files.
I am trying to diagnose an error thrown by this loop. The error is: "Unable to perform assignment because the left and right sides have a different number of elements." The for-loop will run until it finally hits an error, so I know that the code at least somewhat works. I need to either:
  1. Figure out what is throwing the error and redraw the code such that it doesn't throw this error, or
  2. Figure out how to write the code such that it skips the error.
The following is the for-loop that is proving problematic. If it would be helpful to have my full code and text files, please let me know.
m = NaN(length(gp2_addon),1);
n = NaN(length(gp2_addon),1);
%test for-loop
tolerance = 1*10^-15;
for i = 1:length(gp2_addon)
[m(i),n(i)] = find(abs(Rover(:,1)-gp2(i,1))<=tolerance);
gp2(i,6)=Rover(m(i),2);%corrected latitude
gp2(i,7) = Rover(m(i),3);%corrected longitude
end
For the code:
m and n are two vectors in which the indicies from the find function are stored.
Rover is the PPK solution and gp2 is the original uncorrected coordinates.
The tolerance is set for decimal time - both files utilize a time stamp rounded to 15 decimal places.
The line that specifically throws the error is [m(i),n(i)]=find...
What I suspect is going on:
I think the find function is not finding a match in the instances where it throws the error. In this instance, it comes up empty and replaces m(i) and n(i) with a blank set.
Thank you so much for your time!

채택된 답변

dpb
dpb 2019년 9월 28일
find will return a variable number of outputs depending upon the inputs and you've written the code with the presumption of only a single match. Your code will error whenever that isn't so whether it fails to find a match or there is (possibly?) more than one.
If it isn't possible to have more than one, then you can do something like
[im,in] = find(abs(Rover(:,1)-gp2(i,1))<=tolerance);
if ~isempty(im)
m(i)=im; n(i)=in;
end
or alternatively, put the find call in try...catch construct since you've already initialized the result array
try
[m(i),n(i)] = find(abs(Rover(:,1)-gp2(i,1))<=tolerance);
catch
end
which will just continue after the error.
If it is possible to have multiple matches, then you either need to restrict find to only return one or you'll have to use a cell array to hold the results or similar construct.
  댓글 수: 4
Randall Bonnell
Randall Bonnell 2019년 9월 29일
Thanks again for your helpful comments.
I have a smooth code now! I eliminated non-unique values from the PPK-solution file and used the ~isempty method to correct the uncorrected file.
dpb
dpb 2019년 10월 1일
That would appear to return the same result as using
[m(i),n(i)] = find(abs(Rover(:,1)-gp2(i,1))<=tolerance,1);
excepting wouldn't have to modify the initial file.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by