フィルターのクリア

Extract sub-matrices from a big matrix

1 回表示 (過去 30 日間)
androidguy
androidguy 2017 年 3 月 29 日
コメント済み: androidguy 2017 年 3 月 29 日
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!

採用された回答

Jan
Jan 2017 年 3 月 29 日
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 件のコメント
androidguy
androidguy 2017 年 3 月 29 日
That works! Thank you so much for the elegant answer!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by