Restructuring a matrix based on an index

1 次查看(过去 30 天)
I have a 60x12 matrix in a table format (with column headers), where the first three columns are identification indices: site (1, 2, 3), species (1, 2, 3), and individual ID (1, 2, ...10), while the remaining nine columns are measured data (height, diameter etc.). I want to restructure the matrix so that site and species remain as columns, whereas the individual ID (within each combination of site and species) is used as an index in the nine columns with measured variables (which would turn into 9 * 10). So instead of height, diameter etc., I would get height_ID1, ... height_ID10, diameter_ID1, ... diameter_ID10 etc. What complicates things (perhaps) is that:
1) for each site and species, individual IDs are 1, 2, ...10. I want to unstack data based on individual IDs within each combination of species and site (not aggregate them together), i.e. keep all combinations of site and species in columns 1 and 2,
2) in some cases, there are two or more rows with the same combination of site, species and individual ID, e.g. in case of diameter which was measured in two directions, or other parameters which were sampled a few times.
Is there an easy function which will let me restructure my data in such a way, and adjust variable names accordingly (by combining original variable name with ID+number, i.e. height_ID1)?
Many thanks in advance for help!
  3 个评论
Corymbiamaculata
Corymbiamaculata 2023-6-8
Thanks! I tried using it, but for some reason some of the combinations of site and species are lost in the unstacked table. Do you know if it's possible to specify variables (i.e. site and species) which should remain... unstacked?
Cris LaPierre
Cris LaPierre 2023-6-9
There is not an option for that. A workaround might be to create a new table variable that does create a unique value for each row you want to keep, and use that to unstack your table.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by