Azzera filtri
Azzera filtri

Extract sub-matrices from a big matrix

1 visualizzazione (ultimi 30 giorni)
androidguy
androidguy il 29 Mar 2017
Commentato: androidguy il 29 Mar 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!

Risposta accettata

Jan
Jan il 29 Mar 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.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by