Question

I have data set like:

101 14.87
104 18.23
110 19.03
113 19.84

and want to put it into this form:

101 14.87
102
103
104 18.23
105
106
107
108
109
110 19.03
111
112
113 19.84

any help? Thanks

Was it helpful?

Solution

If the first column contains non-repeated integers, you can use accumarray, which lets you specify the fill value (I'm using NaN):

result = [(min(A):max(A)).' accumarray(A(:,1)-min(A(:,1))+1, A(:,2), [], @sum, NaN)];

Alternatively, you can use ismember:

indices = min(A(:,1)):max(A(:,1));
result = [indices(:) NaN(numel(indices), 1)];
result(ismember(indices, A(:,1) ),2) = A(:,2);

Either of the above gives

result =

  101.0000   14.8700
  102.0000       NaN
  103.0000       NaN
  104.0000   18.2300
  105.0000       NaN
  106.0000       NaN
  107.0000       NaN
  108.0000       NaN
  109.0000       NaN
  110.0000   19.0300
  111.0000       NaN
  112.0000       NaN
  113.0000   19.8400

OTHER TIPS

Here is a simple way to do it:

x=[101 14.87
104 18.23
110 19.03
113 19.84]

y=(min(x(:,1)):max(x(:,1)))'
y(x(:,1)-min(x(:,1))+1,2)=x(:,2)

Note that this assumes integer values in the first column of x (otherwise it would not be properly defined what you would want).

You may also want to padd with NaN instead of zeros, this can be done by initializing the second column of y properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top