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

3 vues (au cours des 30 derniers jours)
Daichi
Daichi le 17 Juil 2017
Commenté : Daichi le 17 Juil 2017
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 commentaires
Stephen23
Stephen23 le 17 Juil 2017
Modifié(e) : Stephen23 le 17 Juil 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 le 17 Juil 2017
Thank you for your kind pointing out, and so sorry for confusing you!

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 17 Juil 2017
Modifié(e) : Stephen23 le 17 Juil 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 commentaires
Daichi
Daichi le 17 Juil 2017
Modifié(e) : Daichi le 17 Juil 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 le 17 Juil 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]);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 行列および配列 dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!