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

5 次查看(过去 30 天)
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.

请先登录,再进行评论。

采纳的回答

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 CenterFile Exchange 中查找有关 行列および配列 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!