How do I split an array into small array of fixed size with increments by 1?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alekhya Hati
am 28 Okt. 2022
Kommentiert: Bruno Luong
am 28 Okt. 2022
For example, I have an array, say
aa = [2 5 6 10 2 4 2 1 -1 -12 0 3 7];
What I want to do is create arrays of length 4, such as
[2 5 6 10], [5 6 10 2], [6 10 2 4], [10 2 4 2], [2 4 2 1], [4 2 1 -1], ....
and group them into a matrix. I understand this can be done in a for loop, but is there a way to vectorize this process?
0 Kommentare
Akzeptierte Antwort
Bruno Luong
am 28 Okt. 2022
A=randi(10,1,10)
k=4;
B = hankel(A(1:end-k+1),A(end-k+1:end))
3 Kommentare
Alekhya Hati
am 28 Okt. 2022
Bearbeitet: Alekhya Hati
am 28 Okt. 2022
Bruno Luong
am 28 Okt. 2022
I never use this command buffer, so I learn something.
It requires the signal processing toolbox though.
Weitere Antworten (1)
DGM
am 28 Okt. 2022
Bearbeitet: DGM
am 28 Okt. 2022
I'm sure there are other ways, but here's one way.
% the input
A = [2 5 6 10 2 4 2 1 -1 -12 0 3 7];
% the parameter
blocklen = 4;
% generate a 2D array of indices
nrows = numel(A)-blocklen+1;
idx = (1:blocklen) + (0:nrows-1).';
% build the output by direct addressing
B = A(idx)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!