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

5 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Matt J
Matt J el 24 de Oct. de 2021
Editada: Matt J el 24 de Oct. de 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 comentarios
Matt J
Matt J el 24 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by