Fill data with NaN for missing days

2 views (last 30 days)
Sagar
Sagar on 25 Dec 2017
Edited: Sagar on 25 Dec 2017
I have a matrix A in which I have number of days in first column and corresponding data in the second column. For example, A (1,1) = 1; A (1,2) = 0.1; A (2, 1) = 31; A (2, 2) = 0.2; A (3, 1) = 365; A (3, 2) = 0.25; The values in the first column 1, 31, and 365 are the days of a year i.e. January 1, January 31, and December 31, respectively. The values in the second column are the data for the corresponding days. For many days of the year, data is not available so the matrix A is incomplete. Now I want to create another complete matrix of size 365*2 (365 is the total no. of days in a year) in which I want to fill the rows with corresponding data from A when the data is available and fill with NaN when the data is not available. For example, I want to fill all days with NaN except Jan 1, Jan 31, and Dec 31 in this case since I don't have data for those period. Could you please advise a neat way to do this?

Answers (1)

Sagar
Sagar on 25 Dec 2017
Edited: Sagar on 25 Dec 2017
I finally did it although using a loop:
%create a index column first
first_nan(:, 1) = 1:365;
%compare the A matrix values with NaN matrix and create a new matrix
for i = 1:365;
for j = 1:365;
if( A (j, 1) == first_nan (i, 1));
result (i, :) = A (j, :);
end;
end;
end;

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!