Restructuring a matrix based on an index

조회 수: 1 (최근 30일)
Corymbiamaculata
Corymbiamaculata 2023년 6월 8일
댓글: Cris LaPierre 2023년 6월 9일
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