Fill data with NaN for missing days

1 Ansicht (letzte 30 Tage)
Sagar
Sagar am 25 Dez. 2017
Bearbeitet: Sagar am 25 Dez. 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?

Antworten (1)

Sagar
Sagar am 25 Dez. 2017
Bearbeitet: Sagar am 25 Dez. 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!

Translated by