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

4 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 17 Jul. 2017
Bearbeitet: Stephen23 am 17 Jul. 2017
@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 am 17 Jul. 2017
Thank you for your kind pointing out, and so sorry for confusing you!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 17 Jul. 2017
Bearbeitet: Stephen23 am 17 Jul. 2017
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 Kommentare
Daichi
Daichi am 17 Jul. 2017
Bearbeitet: Daichi am 17 Jul. 2017
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 am 17 Jul. 2017
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]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 行列および配列 finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!