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

5 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating 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