Hi, How to write 3 or 4 dimansion array?
Thanks, Henry

 Accepted Answer

Stephen23
Stephen23 on 11 Sep 2015
Edited: Stephen23 on 11 Sep 2015
Any internet search engine will give these pages at the top of the search results:
MATLAB's documentation is really useful and readable. It tells us how to use MATLAB. It has working examples too. You can search it using your favorite internet search engine: this will find the official, correct and recommended ways of using MATLAB. The first link above includes the title "Creating Multi-Dimensional Arrays" at the top of the page. Perhaps you might like to read this.
Here are three easy ways to create ND arrays:
  • indexing:
>> A(:,:,2) = [1,2;3,4];
>> A(:,:,1) = [9,8;7,6]
A(:,:,1) =
9 8
7 6
A(:,:,2) =
1 2
3 4
  • reshape:
>> X = [9,7,8,6,1,3,2,4];
>> reshape(X,2,2,2)
ans(:,:,1) =
9 8
7 6
ans(:,:,2) =
1 2
3 4
  • cat:
>> X = [1,2;3,4];
>> Y = [9,8;7,6];
>> cat(3,Y,X)
ans(:,:,1) =
9 8
7 6
ans(:,:,2) =
1 2
3 4

6 Comments

Henry Buck
Henry Buck on 11 Sep 2015
Thanks a lot, Henry
Luis
Luis on 15 Jun 2026
in this example you shows a 2 dimensional array?
Those approaches do start off using 2-dimensional arrays.
The indexing approach assigns those 2-dimensional arrays into segments of a 3-dimensional array.
The reshape approach "folds" the 2-dimensional arrays in the third dimension. [If you've ever seen those toys that are snakes built of plastic or wooden blocks, picture folding one of those into a cube form. I'm thinking of something like a Rubik's Snake.]
The cat approach stacks two 2-dimensional arrays on top of one another, like snapping one Lego block on top of another Lego block.
A fourth approach, that's somewhat related to the indexing approach, is the allocate-and-fill approach. In this you "set up shelves" using a function like zeros, ones, etc. then assign values into the array like putting items on the shelves.
shelves = zeros(2, 3, 4)
shelves =
shelves(:,:,1) = 0 0 0 0 0 0 shelves(:,:,2) = 0 0 0 0 0 0 shelves(:,:,3) = 0 0 0 0 0 0 shelves(:,:,4) = 0 0 0 0 0 0
shelves(1, 2, :) = [1 2 3 4]
shelves =
shelves(:,:,1) = 0 1 0 0 0 0 shelves(:,:,2) = 0 2 0 0 0 0 shelves(:,:,3) = 0 3 0 0 0 0 shelves(:,:,4) = 0 4 0 0 0 0
I've essentially put stuff in the "middle of the back of each shelf" with the assignment statement. This approach makes sure I have the right sized "set of shelves" from the get-go without the trick the indexing approach used, where it put items on the second shelf first (to make sure you got a shelving unit with at least two shelves.)
To be explicit:
There is no MATLAB notation to directly represent 3 or more dimensions of an array. For example,
A = [[1 2 3], [4 5 6]]
does NOT create a 2 x 1 x 3 array, and there is no notation similar to
A = [1 2 3 ## 4 5 6]
to create a 1 x 3 x 2 array.
The closest MATLAB notation to that is
A = cat(3, 1 2 3, 4 5 6)
The direct MATLAB notation only extends to 2 dimensions.
A = cat(3, [1,2,3], [4,5,6])
Opps,
A = cat(3, [1 2 3], [4 5 6])
it is!

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!