How to covert a 1D vector to 2D matrix with overlapping effectively?

5 ビュー (過去 30 日間)
Kim
Kim 2021 年 10 月 24 日
コメント済み: Kim 2021 年 10 月 24 日
Dear Experts,
I would like to make a function to covert a 1D vector to a 2D matrix form with overlapping.
==
X = [1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
segment_size = 4; overlap_rate = 0.5
== Expected Results ===
[ 1 3 5 ... 12
2 4 6 ... 13
3 5 7 ... 14
4 6 8 ... 15]
It would be good to use some functions instead of using "for loop".
Thank you

採用された回答

Matt J
Matt J 2021 年 10 月 24 日
編集済み: Matt J 2021 年 10 月 24 日
X=randi(30, 1,14);
segment_size = 4;
overlap_rate = 0.5;
d=segment_size*overlap_rate;
idx=(1:segment_size)'+ ( 0:d:(numel(X)-segment_size) );
[~,rec]=unique(idx);
rec=rec(:).';
X,
X = 1×14
9 13 10 9 4 30 12 28 27 20 18 20 12 18
Matrix=X(idx)
Matrix = 4×6
9 10 4 12 27 18 13 9 30 28 20 20 10 4 12 27 18 12 9 30 28 20 20 18
Xrec=Matrix(rec)
Xrec = 1×14
9 13 10 9 4 30 12 28 27 20 18 20 12 18
  3 件のコメント
Matt J
Matt J 2021 年 10 月 24 日
Thank you for your prompt reply.
You're welcome, but please Accept-click the answer if it fulfills your question.
Then, can you please let me know how to return the 2-D matrix back to a 1-D vector?
I will add it to my Answer.
Kim
Kim 2021 年 10 月 24 日
Thanks a million!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by