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"

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!

 Accepted Answer

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 Comments

You're amazing! Thank you very much for your help.
I've tried the ~isempty option and that yielded the same error. I do not think values are necessarily unique in the dataset.
The try/catch method worked! I do feel as though this is more of a brute force method and would prefer a simpler method if possible.
Do you have any thoughts on using find when there are non-unique matches?
Thank you again for your help!
"...values are [not] necessarily unique in the dataset."
In that case it all depends upon what the wanted behavior should be -- we can't know that, only you can decide what should the result then be.
try...catch is a very workable and useful solution if the desired result is to only return elements with a single match and nothing for the others (or, of course, you can put another action in the catch clause).
"thoughts on using find when there are non-unique matches?"
Well, there's the optional count parameter that will limit the returned output to the number requested. But, again, that's up to what you want the behavior to be and we can't know that without you telling us.
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.
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 27 Sep 2019

Commented:

dpb
on 1 Oct 2019

Community Treasure Hunt

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

Start Hunting!