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
This question is related to How do I combine uneven matrices into a single matrix? but a little bit different. I want to combine n files which have different size. Each file read through loop. How I can get the output as shown below?
for i=1:n
....
inputdata=[code score];
sortdata= sortrows(inp开发者_StackOverflow中文版utdata,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
Instead of
inputdata=[code score];
sortdata = sortrows(inputdata,1);
use
completedata(code, n+1) = score;
This way you are using code
as index to your final array. Initialising completedata
before the loop would probably be a good idea.
completedata = [(1:codemax)', zeros(codemax, n)];
精彩评论