2017-10-14 95 views
0

陣列將不勝感激一些幫助,下面的挑戰:Matlab/Simulink仿真:創建事實表

我從導入數據庫的事實表到一個Matlab表。事實表包含在多個類別觀察序列如下:

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 

我現在需要delinearize事實表,並創建一個矩陣(或另一個表)同類別代表列,即是這樣的:

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中完全實現這一點。

任何幫助將是最受歡迎的。

+0

[相關](https://stackoverflow.com/questions/46682751/efficient-ways-to-append-new-data-in-matlab-with-example -code/46686878#46686878) – rahnema1

+0

這與Simulink有什麼關係? –

回答

1
%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