How can I insert some missing rows to a matrix?

6 ビュー (過去 30 日間)
Szabó-Takács Beáta
Szabó-Takács Beáta 2015 年 9 月 19 日
Dear All, I have a matrix A(10950,33087) where the values are stored (time,coordinates) during 30 year period. In this matrix the leap years are missing. I would like to insert colums in the leap years (29th of February) with value NaN. I tried to solve with the following way:
period={'01-Jan-2071';'31-Dec-2100'};dates=datevec([datenum(period{1}):datenum(period{2})]');
k=find(dates(:,2) ==2 & dates(:,3) ==29);
for i=1:10957
A_2(k,:)=nan;
A_2(i,:)=A(i,:);
end
It seems that A2 is same as A with NaN values in the leap years, but I am not sure that it is the best solution. Could someone write me if this way is correct or offer me a better method? Thank you for your help in advance!

採用された回答

Walter Roberson
Walter Roberson 2015 年 9 月 19 日
A_3 = zeros(size(dates,1), size(A_2,2), class(A_2)); %same type, expanded size
lyidx = dates(:,2) ==2 & dates(:,3) ==29;
A_3(~lyidx,:) = A_2;
A_3(lyidx,:) = nan;
  1 件のコメント
Szabó-Takács Beáta
Szabó-Takács Beáta 2015 年 9 月 19 日
Dear Walter! Thank you very much for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by