Matlab/Simulink:根据事实创建数组 table
Matlab / Simulink : create array from fact table
非常感谢您在以下挑战中提供帮助:
我正在将一个事实 table 从数据库导入 Matlab table。事实 table 由以下几个类别的一系列观察结果组成:
SeqNo Cat Observation
1 A 0.3
1 B 0.5
1 C 0.6
2 B 0.9
2 C 1.0
3 A 1.2
3 C 1.5
我现在需要去线性化事实 table 并创建一个矩阵(或另一个 table),其中类别代表列,即像这样的东西:
Seq A B C
1 0.3 0.5 0.6
2 NaN 0.9 1.0
3 1.2 NaN 1.5
我试过 findgroup 和 split-apply-combine 工作流程,但没有成功。最后,我不得不求助于 SPSS Modeler create 来创建结构正确的 csv 文件以供导入,但需要在 Matlab 或 Simulink 中完全实现。
非常欢迎任何帮助。
%Import table
T=readtable('excelTable.xlsx');
obs_Array=T.Observation;
%Extract unique elements from SeqNo column
seqNo_values=(unique(T.SeqNo));
%Extract unique elements from Cat column
cat_values=(unique(T.Cat));
%Notice that the elements in seqNo_values
%already specify the row of your new matrix
%The index of each element in cat_values
%does the same thing for the columns of your new matrix.
numRows=numel(seqNo_values);
numCols=numel(cat_values);
%Initialize a new, NaN matrix:
reformatted_matrix=NaN(numRows,numCols);
%magic numbers:
seqNo_ColNum=1;
cat_ColNum=2;
for i=1:numel(obs_Array)
target_row=T(i,seqNo_ColNum);
%convert to array for ease of indexing
target_row=table2array(target_row);
%convert to array for ease of indexing
target_cat=table2array(T(i,cat_ColNum));
target_cat=cell2mat(target_cat);
target_col=find([cat_values{:}] == target_cat);
reformatted_matrix(target_row,target_col)=obs_Array(i);
end
reformatted_matrix
输出:
reformatted_matrix =
0.3000 0.5000 0.6000
NaN 0.9000 1.0000
1.2000 NaN 1.5000
非常感谢您在以下挑战中提供帮助:
我正在将一个事实 table 从数据库导入 Matlab table。事实 table 由以下几个类别的一系列观察结果组成:
SeqNo Cat Observation
1 A 0.3
1 B 0.5
1 C 0.6
2 B 0.9
2 C 1.0
3 A 1.2
3 C 1.5
我现在需要去线性化事实 table 并创建一个矩阵(或另一个 table),其中类别代表列,即像这样的东西:
Seq A B C
1 0.3 0.5 0.6
2 NaN 0.9 1.0
3 1.2 NaN 1.5
我试过 findgroup 和 split-apply-combine 工作流程,但没有成功。最后,我不得不求助于 SPSS Modeler create 来创建结构正确的 csv 文件以供导入,但需要在 Matlab 或 Simulink 中完全实现。
非常欢迎任何帮助。
%Import table
T=readtable('excelTable.xlsx');
obs_Array=T.Observation;
%Extract unique elements from SeqNo column
seqNo_values=(unique(T.SeqNo));
%Extract unique elements from Cat column
cat_values=(unique(T.Cat));
%Notice that the elements in seqNo_values
%already specify the row of your new matrix
%The index of each element in cat_values
%does the same thing for the columns of your new matrix.
numRows=numel(seqNo_values);
numCols=numel(cat_values);
%Initialize a new, NaN matrix:
reformatted_matrix=NaN(numRows,numCols);
%magic numbers:
seqNo_ColNum=1;
cat_ColNum=2;
for i=1:numel(obs_Array)
target_row=T(i,seqNo_ColNum);
%convert to array for ease of indexing
target_row=table2array(target_row);
%convert to array for ease of indexing
target_cat=table2array(T(i,cat_ColNum));
target_cat=cell2mat(target_cat);
target_col=find([cat_values{:}] == target_cat);
reformatted_matrix(target_row,target_col)=obs_Array(i);
end
reformatted_matrix
输出:
reformatted_matrix =
0.3000 0.5000 0.6000
NaN 0.9000 1.0000
1.2000 NaN 1.5000