Fill data with NaN for missing days

조회 수: 2 (최근 30일)
Sagar
Sagar 2017년 12월 25일
편집: Sagar 2017년 12월 25일
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?

답변 (1개)

Sagar
Sagar 2017년 12월 25일
편집: Sagar 2017년 12월 25일
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;

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by