How to shift certain arrays in a matrix downward

4 ビュー (過去 30 日間)
SMA
SMA 2016 年 2 月 9 日
コメント済み: SMA 2016 年 2 月 9 日
I have a large matrix where data for each month of 100 years is stored for 12 variables. Some of the variables have data starting from later date. How can a shift those variables down. For example I have a matrix;
1 4 6 9 0 2
3 6 8 8 2 5
2 5 6 8 9 0
3 5 7 9 2 1
And I'd like the output to be (with NaNs replacing the empty places);
1 4 N N N 2
3 6 6 N N 5
2 5 8 9 0 0
3 5 6 8 2 1
Provided I have stored in a different matrix what the lag would be, in this case say lag=[0, 0, 1, 2, 2, 0].

採用された回答

Jos (10584)
Jos (10584) 2016 年 2 月 9 日
This is a fast and easy-to-read solution:
M = [1 4 6 9 0 2
3 6 8 8 2 5
2 5 6 8 9 0
3 5 7 9 2 1] % matrix
S = [0 0 1 2 2 0] % shift for each column
M2 = nan(size(M)) ; % pre-allocation for speed
for k=1:size(M,2) % loop over columns
M2(1+S(k):end, k) = M(1:end-S(k), k) ; % copy column k with a shift
end
disp(M2)
  1 件のコメント
SMA
SMA 2016 年 2 月 9 日
Great, I was doing something similar but messed up somewhere. It works with minor modification for my data.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by