2010-07-04 62 views
0
student1  student2  student3 
code score code score code score 
1  20  1  100 1  22 
2  11  3  11  2  90 
3  12  4  22  5  11 
4  11 
5  28 

此問題與How do I combine uneven matrices into a single matrix?有關,但有一點不同。我想結合具有不同大小的n個文件。每個文件通過循環讀取。我如何得到如下所示的輸出?Matlab combined matrix

for i=1:n 
    .... 
    inputdata=[code score]; 
    sortdata= sortrows(inputdata,1); 
end 

Output 
code s1 s2 s3 
1 20 100 22 
2 11 0 90 
3 12 11 0 
4 11 22 0 
5 28 0 11 

回答

1

而不是

inputdata=[code score]; 
sortdata = sortrows(inputdata,1); 

使用

completedata(code, n+1) = score; 

您使用code作爲索引你的最後一個數組這樣。在循環之前初始化completedata可能是一個好主意。

completedata = [(1:codemax)', zeros(codemax, n)]; 
+0

什麼是codemax? – Jessy 2010-07-04 17:54:58

+0

它包含'code'可以包含的最大數量,在你的情況下'codemax = 5'。如果你事先不知道它,你就不需要它,但是你的'completedata'必須在for循環內部增長,這可能是一個性能瓶頸。 – groovingandi 2010-07-04 18:01:51

+0

謝謝。我的問題現在解決了:) .. – Jessy 2010-07-04 18:18:08