Filter löschen
Filter löschen

Extract sub-matrices from a big matrix

1 Ansicht (letzte 30 Tage)
androidguy
androidguy am 29 Mär. 2017
Kommentiert: androidguy am 29 Mär. 2017
I have an example matrix like the one given below. Original size of my matrix is (83193 x 2). I want extract every 2 rows and make a 2 x 2 matrix from this big matrix. For example:
[1 2
3 4
5 6
7 8
9 10
11 12]
Changed into:
A = [1 2; 3 4]
B = [5 6; 7 8]
C = [9 10; 11 12]
How can I do this operation?
Thanks!

Akzeptierte Antwort

Jan
Jan am 29 Mär. 2017
X = rand(83193, 2);
s1 = size(X, 1);
XX = X(1:s1 - rem(s1, 2), :); % Number of blocks needs to be multiple of 2
Y = permute(reshape(XX, 2, [], 2), [1, 3, 2]);
Now Y(:, :, i) is the i.th 2x2 matrix. This is much more efficient than creating 41596 variables.
  1 Kommentar
androidguy
androidguy am 29 Mär. 2017
That works! Thank you so much for the elegant answer!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by