For loop not working on logical indexing
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a vector and a matrix. The vector is distance (22 elements long) and the logical matrix is LandingVelocities_index (22x548) which finds the max velocity in MeanVelocityPerBox (also 22x548). I'm trying to find the speed of particles that correspond to a distance based on their row (1 thru 22). Doing this manually works, but doing it in a for loop gives me an error (Subscripted assignment dimension mismatch.)
Manually, this works
LandingVelocityDistance(1,4)=distance(find(LandingVelocities_index(:,4)==1))
where the "4" could be any column 1-548.
In this loop, it does not work:
LandingVelocityDistance=ones(1,length(MeanVelocityPerBox)); %Pre-allocate space
for h=1:length(MeanVelocityPerBox)
LandingVelocityDistance(1,h)=distance(find(LandingVelocities_index(:,h)==1));
end
Ultimately, I want to end up with two vectors of the same length that allow me to plot max velocity versus distance. Any tips appreciated.
0 comentarios
Respuestas (1)
Adam
el 13 de Mzo. de 2017
I suspect there are quicker ways than a for loop, but just focussing on your code, I suspect in some cases 'find' is returning multiple results.
doc find
mentions a second argument to the function which you should use (and you ought to have find underlined in that code with a message if you hover over it telling you this).
find( LandingVelocities_index(:,h)==1, 1 )
will return only the first element it finds matching the criteria. Of course if it isn't only the first you want then that is a different matter and your code would have to be able to handle that on the left side of your assignment (e.g. using a cell array to accommodate variable length answers)
2 comentarios
Adam
el 13 de Mzo. de 2017
Editada: Adam
el 13 de Mzo. de 2017
Well, find is either returning too many results or none to be getting a subscripted assignment dimension mismatch error.
Using the debugger and the 'Stop on Errors' option from the breakpoints menu in the editor will make it trivial to understand what is going wrong. Or
dbstop if error
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!