필터 지우기
필터 지우기

How to transpose every cell in a table?

조회 수: 22 (최근 30일)
Karol P.
Karol P. 2023년 3월 18일
편집: Karol P. 2023년 3월 18일
I have a table that contain array of double in every cell. It looks like this:
Column1 Column2
____________ ________________
1×7 double 1×27 double
1×7 double 1×27 double
1×7 double 1×27 double
1×7 double 1×27 double
1×7 double 1×27 double
But I need every cell to be 7x1 or 27x1, respectively (data in column vector, not row vector). How can I transpose it?
  댓글 수: 1
the cyclist
the cyclist 2023년 3월 18일
Are your data truly in a table data object? Or possibly in a cell array (which is arguably the more common method for storing data like this)?

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2023년 3월 18일
편집: the cyclist 2023년 3월 18일
% If data are in a cell array
c = {rand(1,7),rand(1,27)}
c = 1×2 cell array
{[0.2034 0.5277 0.8087 0.4063 0.0021 0.1522 0.6562]} {[0.3394 0.3716 0.9851 0.2202 0.6112 0.0657 0.3078 0.1728 0.0608 0.9520 0.0747 0.5836 … ]}
ctrans = cellfun(@transpose,c,'UniformOutput',false)
ctrans = 1×2 cell array
{7×1 double} {27×1 double}
% If data are in a table, convert to cell array, transpose, and convert back
t = table(rand(1,7), rand(1,27), 'VariableNames', {'t1','t2'})
t = 1×2 table
t1 t2 __________ ___________ 1×7 double 1×27 double
ct = table2cell(t);
cttrans = cellfun(@transpose,ct,'UniformOutput',false);
ttrans = cell2table(cttrans)
ttrans = 1×2 table
cttrans1 cttrans2 ____________ _____________ {7×1 double} {27×1 double}
There might be a more direct way to do this operation on a table, but I didn't think of one. Also, you'll need to rename the table variables.
  댓글 수: 1
Karol P.
Karol P. 2023년 3월 18일
편집: Karol P. 2023년 3월 18일
The data are in table format, at least according to "Value" field in workspace. I know it is less common for data like this, but it is a part of bigger code. Anyway, the second solution with conversion to cell, and then cellfun, works fine, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by