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
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 Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!