Repeat the concatenation of every n rows

4 ビュー (過去 30 日間)
Jared Wilson
Jared Wilson 2019 年 4 月 12 日
コメント済み: Jared Wilson 2019 年 4 月 12 日
I have a large data set in which there are 178 recordings for every 1 second displayed in 1 row. I want to concatenate 23 rows at a time, to create rows that have 23 second time intervals. In total i need to create 500 of these 23 second rows. I have the following code for the horizontal concatenation of 23 rows:
for i = 1
section1 = horzcat(data(i,1:178),data(i+1,1:178),data(i+2,1:178),data(i+3,1:178),data(i+4,1:178),data(i+5,1:178),data(i+6,1:178),data(i+7,1:178),data(i+8,1:178),data(i+9,1:178),data(i+10,1:178),data(i+11,1:178),data(i+12,1:178),data(i+13,1:178),data(i+14,1:178),data(i+15,1:178),data(i+16,1:178),data(i+17,1:178),data(i+18,1:178),data(i+19,1:178),data(i+20,1:178),data(i+21,1:178),data(i+22,1:178));
end
I want to do something like this for the remaining sections:
s = ((i-1)/23)+1
while s <= 500
for i = i + 23
section(s) = horzcat(data(i,1:178),data(i+1,1:178),data(i+2,1:178),data(i+3,1:178),data(i+4,1:178),data(i+5,1:178),data(i+6,1:178),data(i+7,1:178),data(i+8,1:178),data(i+9,1:178),data(i+10,1:178),data(i+11,1:178),data(i+12,1:178),data(i+13,1:178),data(i+14,1:178),data(i+15,1:178),data(i+16,1:178),data(i+17,1:178),data(i+18,1:178),data(i+19,1:178),data(i+20,1:178),data(i+21,1:178),data(i+22,1:178));
end
end
I'm not that familiar with indexing and am getting errors for this. If anyone could help it would be greatly appreciated!

採用された回答

Jon
Jon 2019 年 4 月 12 日
I think this should do what you want:
s = reshape(data',178*23,500)'
Note the use of the transpose (accent symbol) first to transpose data, and then to transpose the result of the reshaping.
The reshaping does this kind of thing nicely, but MATLAB indexes data columnwise, and you are looking at the data rowwise. So you first need transpose to get the rows into columns (so things will be columnwise) and then at the end you transpose back.
  1 件のコメント
Jared Wilson
Jared Wilson 2019 年 4 月 12 日
Yes! This is exactly what I wanted! Every time I try an do some long code, I just know there has to be a one line solution!
Thanks a ton!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by