Borrar filtros
Borrar filtros

How do I separate a data set into separate cell arrays according to the integer on the end of a string?

3 visualizaciones (últimos 30 días)
Lets say I have a large data set of type "cell" set out in the following way..
{[0.9335302759]} {[0.3892578745]} {[0.37648235478]} {[thing0]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[thing0]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[other0]}
{[0.9335302759]} {[0.3892578745]} {[0.37648235478]} {[thing2]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[thing4]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[other4]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[other4]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[thing49]}
{[0.8965743754]} {[0.5945034750]} {[0.58343053465]} {[other49]}
Is there a way to separate the rows corresponding to each integer on the end of the string in the 4th column into separate cell arrays? So for the data above the two values in thing0 and the one in other0 will be in a single cell array, then a 0 since nothing fell under 1, then "thing2" in another, then a 0 for 3, then "thing4" and "other4" in another, and so forth.
I'm not quite sure where to start with this.. hopefully the way I explained it makes sense.
  1 comentario
Bryan
Bryan el 12 de Mzo. de 2020
Editada: Bryan el 12 de Mzo. de 2020
lastint = NaN(size(yourcell,1),1)
for i = 1:size(yourcell,1)
lastint(i) = str2num(yourcell{i,end}(end))
end
% get only the stuff ending with zero
zerostuff = yourcell{lastint==0,:}
Something like that. Wrote on my phone so haven't been able to test it

Iniciar sesión para comentar.

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 12 de Mzo. de 2020
Editada: Mohammad Sami el 12 de Mzo. de 2020
You need to extract the digit at the end.
% c = yourcell array
digit = regexp(c(:,4),'\d+$','match','once');
[u_d,i,j] = unique(digit);
outcell = arrayfun(@(x)c(j==x,:),i,'UniformOutput',false);
  5 comentarios
mel1708
mel1708 el 13 de Mzo. de 2020
Hi Mohammad,
For the line outcell(i,:) = outcell(j,:) Matlab gives me the error "Index in position 1 is invalid. Array indices must be positive integers or logical values.", which I'm guessing has to do with the fact it thinks I am indexing with j and hence has an issue with the first integer being 0. Would you be able to suggest a fix for this?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by