Split arrays in table into separate rows

Hello everyone,
I have a table - see screenshot for clarification - where some columns consist of one array each, e.g. the columns R2s and MT. One column contains an array with coordinates x, y and z for each of the items in the following arrays, so 6 sets of coordinates in this case in the first row. The other columns contain strings or single numbers, e.g. 'number_of_voxels'.
What I would like to do is to split the arrays and put each item with its corresponding items from all other columns - including the coordinate column - into one row, e.g.
voxel_coord_x voxel_coord_y voxel_coord_z R2s MT number_of_voxels
11 18 30 0.0124 1.6732 6
12 18 32 0.0162 1.4217 6
This would mean that the array with coordinates doesn't only need to be split into separate rows, but also into three columns. Also, I would need the columns with single strings to get repeated for rows that were in the same array, e.g. 'number_of_voxels' in this example.
I know strsplit could be used to separate the items at the semicolon, but I cannot figure out how to move everything into separate rows/columns. Note that not all arrays have the same number of items.
Thank you in advance for any help or tips!

 Respuesta aceptada

Duncan Po
Duncan Po el 22 de Oct. de 2021
I think you can do what you want in several steps.
% Convert your cell arrays in voxel_coord, R2s, and MT into arrays using cell2mat
t1 = varfun(@cell2mat, t, 'InputVariables', {'voxel_coord', 'R2s', 'MT'});
t1.Properties.VariableNames = {'voxel_coord', 'R2s', 'MT'}); % varfun modifies the names, change them back
% Split voxel_coord into multiple columns
t1 = splitvars(t1, 'voxel_coord', 'NewVariableNames', {'voxel_coord_x', 'voxel_coord_y', 'voxel_coord_z'});
% Finally expand number_of_voxels and scale
t1.number_of_voxels = repelem(t.number_of_voxels, 6);
t1.scale = repelem(t.scale, 6);

3 comentarios

Thank you so much, this was a great help and (almost) completes the task perfectly!
The only problem is with the last step, expanding the cells that have to stay the same into several rows (here 'number_of_voxels' and 'scale'). These cells don't need to be copied the same number of times, it is not always 6, so the solution with repelem does not work. I tried
for a = 1:length(t.number_of_voxels)
t1.number_of_voxels(a) = repelem(t.number_of_voxels, a);
end
but of course this is no good because t1 and t don't have the same length so the referencing is not correct. If anyone has an idea that would be wonderful, thank you in advance!
It looks like the number of times you want to copy is equal to number_of_voxels value. Does this work?
% Finally expand number_of_voxels and scale
t1.number_of_voxels = repelem(t.number_of_voxels, t.number_of_voxels);
t1.scale = repelem(t.scale, t.number_of_voxels);
RP
RP el 26 de Oct. de 2021
That's exactly what I wanted, thank you so so much for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

RP
el 21 de Oct. de 2021

Comentada:

RP
el 26 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by