Observation: sortrows() blows up when attempting to sort empty cells
Mostrar comentarios más antiguos
This is more of an observation than a question, but it took me two days to figure it out.
I am pre-allocating space for an array, to save time. The array is about 50,000 rows long, and pre-allocating the cells saves a lot of time processing, as opposed to "growing" the array one row at a time. However, if some of the row cells are not populated, the sortrows function will crash when it gets to the rows containing empty cells.
output = cell(5,5);
output =
5x5 cell array
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
output((1:3),(1:5)) = {'1'}
output =
5x5 cell array
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{'1' } {'1' } {'1' } {'1' } {'1' }
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
sortrows(output)
Error using sort
Cell elements must be character arrays
I'll admit, a more careful job of coding could prevent this from happening. However, it would be nice if the sort and sortrows functions could just take this in stride, rather than crashing.
5 comentarios
"This also happens even if the extra rows are initialized to empty character arrays:"
No, it does not. I have used this exact feature of SORTROWS since atleast R2009b.
What you show are not empty character vectors, but are in fact empty numeric arrays.
"pre-allocating the cells saves a lot of time processing, as opposed to "growing" the array one row at a time."
The you can very easily preallocate with empty character arrays, e.g. using REPMAT or simple assignment:
output = cell(5,5);
output(:) = {''};
... the rest of your code
Steven Lord
el 8 de Dic. de 2022
However, it would be nice if the sort and sortrows functions could just take this in stride, rather than crashing.
First, sort and sortrows do not crash in this circumstance. They throw an error.
Second, what do you propose the functions do to "take this in stride"?
- Should {'1'} comes before or after {[]} in the sorted output?
- How about {'1'} versus {1}?
- {'1'} versus {50}?
- {'5'} versus {1:10}?
Kurt
el 8 de Dic. de 2022
"Yes, it does. Just prior to the sort I merged two arrays vertically. Each of the arrays has empty cells at the end, and at the transition where it crashes the data looks exactly like this in the debug editor. The array is defined as '1000x24 cell'."
The empty cells at the end are very clearly numeric, not character.
The error message tells you that the cell content must be character (but are not).
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!