Réponse acceptée

Stephen23
Stephen23 le 11 Sep 2015
Modifié(e) : Stephen23 le 11 Sep 2015

2 votes

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 commentaires

Henry Buck
Henry Buck le 11 Sep 2015
Thanks a lot, Henry
Luis
Luis le 15 Juin 2026 à 20:17
in this example you shows a 2 dimensional array?
Steven Lord
Steven Lord le 15 Juin 2026 à 20:44
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.)
Walter Roberson
Walter Roberson le 16 Juin 2026 à 18:10
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.
Stephen23
Stephen23 le 17 Juin 2026 à 3:15
A = cat(3, [1,2,3], [4,5,6])
Walter Roberson
Walter Roberson le 17 Juin 2026 à 5:05
Opps,
A = cat(3, [1 2 3], [4 5 6])
it is!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur LEGO MINDSTORMS EV3 Hardware dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by