2 views (last 30 days)
Show older comments
Bruce Elliott on 5 Jan 2024
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction
Commented: Bruce Elliott on 5 Jan 2024
Accepted Answer: John D'Errico
Is there an equivalent function to vertcat(..) and horzcat(..) but in the direction of a third index, e.g. pagecat(..)?
I always accomplish this by horizontally concatenating my arrays and then using permute(..), but it would be convenient to be able to do it with a single function call. Better still, add a third operator to "," and ";" as a shortcut to a pagecat(..) function, if there are any symbols left for that purpose.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
John D'Errico on 5 Jan 2024
⋮
Edited: John D'Errico on 5 Jan 2024
Open in MATLAB Online
Logically, one would just use cat. How might you have found this? READ THE HELP!
That is, near the end of the help for all MATLAB functions, they try to list functions which have similar abilities, are related in some way. That will be a see also line.
help horzcat
HORZCAT Horizontal concatenation. [A B] is the horizontal concatenation of matrices A and B. A and B must have the same number of rows. [A,B] is the same thing. Any number of matrices can be concatenated within one pair of brackets. Horizontal and vertical concatenation can be combined together as in [1 2;3 4]. [A B; C] is allowed if the number of rows of A equals the number of rows of B and the number of columns of A plus the number of columns of B equals the number of columns of C. The matrices in a concatenation expression can themselves by formed via a concatenation as in [A B;[C D]]. These rules generalize in a hopefully obvious way to allow fairly complicated constructions. N-D arrays are concatenated along the second dimension. The first and remaining dimensions must match. C = HORZCAT(A,B) is called for the syntax '[A B]' when A or B is an object. Y = HORZCAT(X1,X2,X3,...) is called for the syntax '[X1 X2 X3 ...]' when any of X1, X2, X3, etc. is an object. See also VERTCAT, CAT. Documentation for horzcat doc horzcat Other uses of horzcat Bluetooth/horzcat categorical/horzcat codistributed/horzcat dataset/horzcat datetime/horzcat dlarray/horzcat double/horzcat duration/horzcat fittype/horzcat gpib/horzcat gpuArray/horzcat i2c/horzcat icdevice/horzcat icgroup/horzcat icsignal/horzcat iddata/horzcat inline/horzcat iviconfigurationstore/horzcat laurentPolynomial/horzcat laurpoly/horzcat matlab.mixin.Heterogeneous/horzcat matlab.mixin.indexing.RedefinesParen/horzcat opcroot/horzcat RandStream/horzcat sensingDictionary/horzcat serial/horzcat sym/horzcat symbolic/horzcat tabular/horzcat tall/horzcat tcpip/horzcat tokenizedDocument/horzcat tscollection/horzcat udp/horzcat videoinput/horzcat visa/horzcat
In the help for hirzcat, for example, you see the line,
See also vertcat, cat.
And while you know what vertcat does, what might cat do?
help cat
CAT Concatenate arrays. CAT(DIM,A,B) concatenates the arrays A and B along the dimension DIM. CAT(2,A,B) is the same as [A,B]. CAT(1,A,B) is the same as [A;B]. B = CAT(DIM,A1,A2,A3,A4,...) concatenates the input arrays A1, A2, etc. along the dimension DIM. When used with comma separated list syntax, CAT(DIM,C{:}) or CAT(DIM,C.FIELD) is a convenient way to concatenate a cell or structure array containing numeric matrices into a single matrix. Examples: a = magic(3); b = pascal(3); c = cat(4,a,b) produces a 3-by-3-by-1-by-2 result and s = {a b}; for i=1:length(s), siz{i} = size(s{i}); end sizes = cat(1,siz{:}) produces a 2-by-2 array of size vectors. See also NUM2CELL. Documentation for cat doc cat Other uses of cat calendarDuration/cat gpuArray/cat categorical/cat inline/cat codistributed/cat InputOutputModel/cat dataset/cat matlab.mixin.Heterogeneous/cat datetime/cat matlab.mixin.indexing.RedefinesParen/cat dlarray/cat RandStream/cat double/cat sym/cat duration/cat tabular/cat fittype/cat tall/cat geopoint/cat tokenizedDocument/cat
Try it out.
A = magic(3);
B = eye(3);
C = cat(3,A,B)
C =
C(:,:,1) = 8 1 6 3 5 7 4 9 2C(:,:,2) = 1 0 0 0 1 0 0 0 1
5 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Bruce Elliott on 5 Jan 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction#comment_3018526
Holy cow!! How did I not know that? It's been a very long time since I ever looked for help with vertcat or horzcat, so I think I had completely forgotten that cat exists.
Thank you for the reminder - this is clearly what I was looking for.
John D'Errico on 5 Jan 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction#comment_3018686
Sometimes cats just hide in plain sight. This must have been one of them. :)
Stephen23 on 5 Jan 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction#comment_3018711
Schrodinger's cat?
Dyuman Joshi on 5 Jan 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction#comment_3018731
Some cats are like voids, you can only find them if they stare back at you.
Bruce Elliott on 5 Jan 2024
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2066791-is-there-a-built-in-function-to-concatenate-arrays-in-the-third-direction#comment_3018776
Thanks to all for giving this question exactly the serious consideration it deserved.
Happy weekend!
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office