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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Randall Bonnell
el 27 de Sept. de 2019
Comentada: dpb
el 1 de Oct. de 2019
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:
- Figure out what is throwing the error and redraw the code such that it doesn't throw this error, or
- 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!
0 comentarios
Respuesta aceptada
dpb
el 28 de Sept. de 2019
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 comentarios
dpb
el 1 de Oct. de 2019
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!