フィルターのクリア

Shift n rows in column to next column by a grouping variable

3 ビュー (過去 30 日間)
NS
NS 2021 年 6 月 1 日
コメント済み: NS 2021 年 6 月 2 日
I wish to shift every 5 rows to the next column as shown below. I have two columns C1 and C2. I want to shift every 5 rows representing a group variable A2 to third columns and 5 rows of group variable A3 to fourth column. Please suggest how to shift n rows in one columns to next column by a increasing number.
My data looks like this-
C1 C2
A1 23
A1 55
A1 45
A1 55
A1 77
A2 56
A2 43
A2 12
A2 33
A2 55
A3 11
A3 22
A3 33
A3 44
A3 55
My desired output is -
C1 C2 C3 C4
A1 23 NA NA
A1 55 NA NA
A1 45 NA NA
A1 55 NA NA
A1 77 NA NA
A2 NA 56 NA
A2 NA 43 NA
A2 NA 12 NA
A2 NA 33 NA
A2 NA 55 NA
A3 NA NA 11
A3 NA NA 22
A3 NA NA 33
A3 NA NA 44
A3 NA NA 55

採用された回答

Jonas
Jonas 2021 年 6 月 1 日
編集済み: Jonas 2021 年 6 月 1 日
quick and dirty example:
a=(1:24)';
grpSize=4;
mat=nan(numel(a),numel(a)/grpSize);
mat(:,1)=a;
for grpNr=1:numel(a)/grpSize
mat((1:grpSize)+grpSize*(grpNr-1),:)=circshift(mat((1:grpSize)+grpSize*(grpNr-1),:),grpNr-1,2);
end
  1 件のコメント
NS
NS 2021 年 6 月 2 日
Thanks a lot sir ! This is giving the desired output.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by