1
votes

I would like to know how to generate a 3-d array from a 2-d array in matlab. My lack of understanding may simply be the result of not knowing the correct nomenclature.

I have a 2-dimensional array or matrix, A:

A = [12, 62, 93, -8, 22; 16, 2, 87, 43, 91; -4, 17, -72, 95, 6] 

and I would like to add a 3rd dimension with the same values such that:

A(:,:,1) = 12    62    93    -8    22
           16     2    87    43    91
           -4    17   -72    95     6

and

A(:,:,2) = 12    62    93    -8    22
           16     2    87    43    91
           -4    17   -72    95     6

to

A(:,:,p) = 12    62    93    -8    22
           16     2    87    43    91
           -4    17   -72    95     6

how would I go about doing so in the most efficient way (I might have a much larger array where m = 100, n = 50, p= 1000 where A(m,n,p).

1
type "doc repmat" in your Matlab command window, i do not know if it is the most efficient, but the resulting code will be easy to read and understand. - Adrien

1 Answers

2
votes

Try

 result = reshape(repmat(A,1,p),m,n,p)