How to efficiently split a 2-D matrix? (inversion of "vertcat" process).

3 ビュー (過去 30 日間)
Daichi
Daichi 2017 年 7 月 17 日
コメント済み: Daichi 2017 年 7 月 17 日
I have a 2-D matrix A whose size is (N*P x N). This is a vertically concatenated matrix of (N x N) small matrices. Namely,
A = vertcat(B1, B2, ..., BP)
Now, I want to create 3-D matrix C, which has B1-BP in the third dimension. So the size of C is (N x N x P). This is an inversion process of "vertcat". How can I create C without using for loop and of cause without knowing B1-BP? In my application, N is small but P is really large. So looping w.r.t. P is much slower I guess.
I tried the following reshaping.
C = reshape(A, [N, N, P]);
But the result was not correct. Thank you in advance.
  2 件のコメント
Stephen23
Stephen23 2017 年 7 月 17 日
編集済み: Stephen23 2017 年 7 月 17 日
@Daichi: your question and title are contradictory: "How to efficiently split a 2-D matrix? (inversion of "vertcat" process)," but in the description you describe that you want to reshape or permute an array, not to split it. What you have described produces one array from one array, and so is not the "inversion of vertcat". To "split" an array would require num2cell, mat2cell, accumarray or the like, and can produce multiple arrays from one array, and would be the "inversion of vertcat" which produces one array from multiple arrays.
Daichi
Daichi 2017 年 7 月 17 日
Thank you for your kind pointing out, and so sorry for confusing you!

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

採用された回答

Stephen23
Stephen23 2017 年 7 月 17 日
編集済み: Stephen23 2017 年 7 月 17 日
Method one: cat. Avoid the whole problem by concatenating along the third dimension right from the start:
cat(3, B1, B2, ..., BP)
Method two: first transpose and then reshape:
reshape(B.', N, N, [])
For example:
>> B = vertcat( [1,2;3,4], [5,6;7,8])
B =
1 2
3 4
5 6
7 8
>> reshape(B.',2,2,[])
ans(:,:,1) =
1 3
2 4
ans(:,:,2) =
5 7
6 8
  2 件のコメント
Daichi
Daichi 2017 年 7 月 17 日
編集済み: Daichi 2017 年 7 月 17 日
Thank you, you are right. However, the matrix A is obtained from an efficient calculation of sparse block-diagonal matrix. What I really wanted to do is the following page-wise matrix inverse calculation.
M = rand(N,N,P);
X1 = zeros(N,N,P);
for k = 1:P
X1(:,:,k) = M(:,:,k)\eye(N);
end
But this is slow when P increase. So I used a sparse block-diagonal matrix as follows:
I = repmat(reshape(1:N*P,[N,1,P]),[1 N 1]);
J = repmat(reshape(1:N*P,[1,N,P]),[N 1 1]);
A = sparse(I(:),J(:),M(:));
X2 = A\repmat(eye(N),[P,1]);
This is really fast than the previous one when N is small and P is large. The last thing I have to do is to reshape X2 as the same size as X1, which is the problem I asked.
Daichi
Daichi 2017 年 7 月 17 日
Oh, thank you. Your method 2 is really simple answer. I was trapped into a deep thinking and missed such a simple way. Thank you. Now I finished up to calculate the efficient page-wise inverse as follows:
M = rand(N,N,P);
I = repmat(reshape(1:N*P,[N,1,P]),[1 N 1]);
J = repmat(reshape(1:N*P,[1,N,P]),[N 1 1]);
A = sparse(I(:),J(:),M(:));
X2 = reshape((A\repmat(eye(N),[P,1])).', [N, N, P]);
X2 = permute(X2,[2,1,3]);

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange行列および配列 についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!