How to restructure/reshape matrix?

10 vues (au cours des 30 derniers jours)
Dionysius Bryan
Dionysius Bryan le 5 Oct 2018
Modifié(e) : James Tursa le 5 Oct 2018
Hi, I am still novice using Matlab I wonder if it's possible to restructure matrix from :
a=[1
2
4
5
7
8
10
9
12
19
22
21]
to be like this:
a= [1 2 4;
2 4 5;
4 5 7;
5 7 8;
7 8 10;
8 10 9;
10 9 12;
9 12 19;
12 19 22;
19 22 21]
Thank You

Réponses (1)

Stephen23
Stephen23 le 5 Oct 2018
Modifié(e) : Stephen23 le 5 Oct 2018
This is easy with hankel, some indexing, and transpose:
>> a = [1,2,4,5,7,8,10,9,12,19,22,21];
>> c = hankel(a(1:3),a(3:end)).'
c =
1 2 4
2 4 5
4 5 7
5 7 8
7 8 10
8 10 9
10 9 12
9 12 19
12 19 22
19 22 21
And compared against your requested output:
>> b = [1,2,4;2,4,5;4,5,7;5,7,8;7,8,10;8,10,9;10,9,12;9,12,19;12,19,22;19,22,21]
b =
1 2 4
2 4 5
4 5 7
5 7 8
7 8 10
8 10 9
10 9 12
9 12 19
12 19 22
19 22 21
>> isequal(b,c)
ans = 1

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by