Is there a built-in function to concatenate arrays in the third direction?

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

John D'Errico
John D'Errico il 5 Gen 2024
Modificato: John D'Errico il 5 Gen 2024
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 2 C(:,:,2) = 1 0 0 0 1 0 0 0 1
  5 Commenti
Dyuman Joshi
Dyuman Joshi il 5 Gen 2024
Some cats are like voids, you can only find them if they stare back at you.
Bruce Elliott
Bruce Elliott il 5 Gen 2024
Thanks to all for giving this question exactly the serious consideration it deserved.
Happy weekend!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by