I have "teste1" cell array column with values from 0 to 5. I want to separate the rows by number (0, 1, 2, 3, 4, 5) into the respective new cell array. How can I proceed?

1 visualización (últimos 30 días)
A new cell with 0's, another one with 1's, and so on..
However, it would be even better if the rows would not change position in the new cells. Like, if the first 0 is in row 1567, in the new cell this value should appear on the same row positio and not in the first row. How can I do this?
  4 comentarios
Walter Roberson
Walter Roberson el 1 de Nov. de 2016
You wrote,
"Like, if the first 0 is in row 1567, in the new cell this value should appear on the same row positio and not in the first row"
So row 1567 needs to appear in row 1567 of the cell that is devoted to containing the values with 0. In that cell, what should appear in rows 1 to 1566, since those corresponded to rows with a different value? What should appear in row 1567 of the cells devoted to 1, to 2, to 3, and so on?
Eduardo Rocha
Eduardo Rocha el 2 de Nov. de 2016
The same number. If its not, in the rows that are not the pretended number, it can appear a '#' or a 'V', per example. It can even be another number, from '6' to '9'. But I think if you write the code to match the same row position, in the other rows would appear something already defined by MATLAB, no?
For every number, it should appear the same number on the same row position. If its not the number, it can just be something..

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Nov. de 2016
colvals = cell2mat(YourCellArray(:,TheAppropriateColumnNumber));
EmptyCellArray = cell(size(YourCellArray));
NewCell = cell(1,6);
for K = 0 : 5
temp = EmptyCellArray;
mask = colvals == K;
temp(mask) = YourCellArray(mask, :);
NewCell{K+1} = temp;
end
Now, NewCell{J} corresponds to column value J-1, and will be a cell array in which the row entries are all empty for the rows where the column value was not J-1 and will copy the row if the column value for the row is J-1 .
This is necessary in order to keep each row at its same location in the destination array.
I suspect this is not what you actually want. I suspect that you want to do the equivalent of sorting by the column value, with all the entries for the same value kept in the same relative order. If that is what you want then,
colvals = cell2mat(YourCellArray(:,TheAppropriateColumnNumber));
[~, sortidx] = sort(colvals, 'stable');
NewCell = YourCellArray(sortidx, :);
  6 comentarios
Eduardo Rocha
Eduardo Rocha el 2 de Nov. de 2016
Sorry for the mistake, but how can I tell the difference? And thank you very much for the help, once again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays 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!

Translated by