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 comentarios

Fangjun Jiang
Fangjun Jiang el 3 de Sept. de 2020
define layer
David Hill
David Hill el 3 de Sept. de 2020
Do you mean something like:
f(1:5,1:5,1)=1;
f(1:5,1:5,2)=2;
f(1:5,1:5,3)=3;
Nicholas Deosaran
Nicholas Deosaran el 3 de Sept. de 2020
Yes, but is there a way to do it with the ones function or no?
Size = [5,6,3];
M = ones(Size) .* permute([1;2;3],[3,2,1]);
Nicholas Deosaran
Nicholas Deosaran el 3 de Sept. de 2020
Thank you!

Iniciar sesión para comentar.

 Respuesta aceptada

Jon
Jon el 3 de Sept. de 2020
Editada: Jon el 3 de Sept. de 2020

1 voto

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 comentarios

Nicholas Deosaran
Nicholas Deosaran el 3 de Sept. de 2020
Jon thank you for that as well!
Nicholas Deosaran
Nicholas Deosaran el 10 de Sept. de 2020
what does the reshape(1:3,1,1,3) fucntion do?
Abdolkarim Mohammadi
Abdolkarim Mohammadi el 11 de Sept. de 2020
A simple way to reach the matrix you wanted is to create a m-by-n-by-k array of ones and then multiply it by a 1-by-1-by-k array of 1 to k. The trick is to first create a 1-by-k array and then permute it to ge the so-called 1-by-1-by-k array. You can permute it in different ways, including the permute() function and reshape().
Jon
Jon el 14 de Sept. de 2020
Editada: Jon el 14 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Sept. de 2020

Editada:

Jon
el 14 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by