# How to make a 3D Matrix

3 views (last 30 days)
Nicholas Deosaran on 3 Sep 2020
Edited: Jon on 14 Sep 2020
How to make a 3D Matrix that contains all ones in the first layer, all twos in the second layer and all threes in the third layer?
##### 5 CommentsShowHide 4 older comments
Nicholas Deosaran on 3 Sep 2020
Thank you!

Sign in to comment.

### Accepted Answer

Jon on 3 Sep 2020
Edited: Jon on 3 Sep 2020
You can also do it like this, for example for a 2 by 4 by 3
A = ones(2,4,3).*reshape(1:3,1,1,3)
##### 4 CommentsShowHide 3 older comments
Jon on 14 Sep 2020
Edited: Jon on 14 Sep 2020
You asked about what the reshape function does.
The reshape(1:3,1,1,3) The first argument 1:3 makes a 3 element row vector with elements [1, 2, 3]the second, third and fourth arguments specify that we want to turn this row vector into a 1 row, by 1 column, by 3 "page" array. So 1 goes in the 1,1,1 location of the first page, 2 goes into th 1,1,2 location on the second page and 3 goes into the 1,1,3 location on the third page. By shaping it this way the array dimension for the multiplication
ones(2,4,3).*reshape(1:3,1,1,3)
Also type doc reshape on the command line to get documentation on the reshape function for further details

Sign in to comment.

### Categories

Find more on Matrix Indexing 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!

Translated by