How to efficiently split a 2-D matrix? (inversion of "vertcat" process).
3 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 17 Jul 2017
Edited: Stephen23
on 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.
Accepted Answer
Stephen23
on 17 Jul 2017
Edited: Stephen23
on 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)
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 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!