views:

35

answers:

1
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 http://stackoverflow.com/questions/3174631/matlab-how-to-combine-uneven-matric-into-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(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  A: 

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)];
groovingandi
what is the codemax?
Jessy
It contains the maximum number that `code` can contain, in your case `codemax=5`. You don't need it if you don't know it in advance, but then your `completedata` has to grow inside the for-loop which could be a performance bottleneck.
groovingandi
thank you. My problem now solved :) ..
Jessy

related questions