Declaring a multidimensional array in a single expression

Let's say I want to create a A3 × 4 × 4 matrix with one statement (i.e., one equality without any concatenations), something like this:

%// This is one continuous row
A = [ [ [3 3 4 4], [8 11 8 7], [4 4 6 7], [4 7 6 6] ];  ...
      [ [3 2 4 2], [9 6 4 12], [4 3 3 4], [5 10 7 3] ]; ...
      [ [2 2 1 2], [3 3 3 2], [2 2 2 2],  [3 3 3 3] ] ]
+3
source share
2 answers

The concatenation[] operator will work only in two dimensions, for example [a b], to combine horizontally or [a; b]to concatenate vertically.To create matrices with higher dimensions , you can use reshapeor initialize the matrix of the desired size, and then fill it with your values. For example, you can do this:

A = reshape([...], [3 4 4]);  % Where "..." is what you have above

Or that:

A = zeros(3, 4, 4);  % Preallocate the matrix
A(:) = [...];        % Where "..." is what you have above
+6
source

cat "" 2-D , :

A = cat(3, ones(4), 2*ones(4), 3*ones(4));

, .

CATLAB

+6

All Articles