필터 지우기
필터 지우기

Restructuring table - converting row values to columns

조회 수: 3 (최근 30일)
Aero Descerazes
Aero Descerazes 2022년 6월 16일
편집: Walter Roberson 2022년 6월 18일
Hello all,
I have a large CSV file with a table like this:
% Original table %
Parameter Order Value
=========================
text1 1 0.884
text1 2 0.851
text1 3 0.827
text1 4 0.823
text1 5 0.825
text1 6 0.825
text2 1 0.797
text2 2 0.763
text2 3 0.769
text2 4 0.754
text2 5 0.720
text2 6 0.705
text2 7 0.686
text3 1 0.696
text3 2 0.696
text3 3 0.671
text3 4 0.643
text3 5 0.630
and so on
I want to restructure the above table into something like shown below:
% New table %
Order text1 text2 text3
=============================
1 0.884 0.797 0.696
2 0.851 0.763 0.696
3 0.827 0.769 0.671
4 0.823 0.754 0.643
5 0.825 0.720 0.630
6 0.825 0.705
7 0.686
Can you please help me acheive this? Thanks in advance!

답변 (2개)

Walter Roberson
Walter Roberson 2022년 6월 16일
편집: Walter Roberson 2022년 6월 18일
  댓글 수: 1
Aero Descerazes
Aero Descerazes 2022년 6월 16일
How do I preserve the order? I need to separate the Parameters column (text rows).

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


Voss
Voss 2022년 6월 18일
% creating a table like the one you have:
Parameter = strcat('text',sprintfc('%d',[1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3]).');
Order = [1:6 1:7 1:5].';
Value = rand(18,1);
T = table(Parameter,Order,Value)
T = 18×3 table
Parameter Order Value _________ _____ _______ {'text1'} 1 0.19863 {'text1'} 2 0.65551 {'text1'} 3 0.48358 {'text1'} 4 0.20768 {'text1'} 5 0.49245 {'text1'} 6 0.76612 {'text2'} 1 0.34492 {'text2'} 2 0.81026 {'text2'} 3 0.65334 {'text2'} 4 0.8077 {'text2'} 5 0.53812 {'text2'} 6 0.63118 {'text2'} 7 0.39556 {'text3'} 1 0.89911 {'text3'} 2 0.85899 {'text3'} 3 0.74091
% group Parameter and Order from the table:
[pg_idx,pg] = findgroups(T.Parameter);
[og_idx,og] = findgroups(T.Order);
% new table has #-of-order-groups rows and 1+(#-of-parameter-groups) columns
% (first column is order groups - remaining columns are initialized to NaNs):
temp = [{og} repmat({NaN(numel(og),1)},1,numel(pg))];
T_new = table(temp{:},'VariableNames',['Order' pg.']);
% place each Value one at a time from the old table into the new table,
% according to the corresponding order group (row in new_T) and parameter
% group (column in new_T, offset by 1):
for jj = 1:size(T,1)
T_new{og_idx(jj),pg_idx(jj)+1} = T{jj,'Value'};
end
T_new
T_new = 7×4 table
Order text1 text2 text3 _____ _______ _______ _______ 1 0.19863 0.34492 0.89911 2 0.65551 0.81026 0.85899 3 0.48358 0.65334 0.74091 4 0.20768 0.8077 0.45111 5 0.49245 0.53812 0.73575 6 0.76612 0.63118 NaN 7 NaN 0.39556 NaN

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by