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

5 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 24 Oct 2021
Edited: Matt J on 24 Oct 2021
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 Comments
Matt J
Matt J on 24 Oct 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!