0
votes

I have a multidimensional Matlab array, which was created with operations using variables created by the ndgrid command:

[Z,R,T,Es]=ndgrid(z,r,t,E)

where z=r=0 and t is a vector of time values and E is a two dimensional matrix of size=[10,80]

I want to reshape it to a three dimensional array of size=[10,80,length(t)], where the vector t is copied as the third dimension for each of the 10*80 values which correspond to the E matrix values.

How can I do that? -since I understand that the reshape function has certain order in which it fills the new array ?

1
What is the current size of the array? - beaker
The order is that given by linear indexing: run down first column first, then move to second column, ..., then move to second third-dim slice, ... - Luis Mendo

1 Answers

0
votes

I think you can achieve this using permute and repmat

let me assume that t is a row vector for convenience.

octave:41> t
t =

   0.932698   0.658920   0.090389   0.314648   0.509211

octave:42> size(E)
ans =

    10   180

octave:43> P = repmat(permute(t,[3,1,2]), [size(E,1), size(E,2), 1]);
octave:44> size(P)
ans =

    10   180     5

octave:45> P(1:3,1:3, :)
ans =

ans(:,:,1) =

   0.93270   0.93270   0.93270
   0.93270   0.93270   0.93270
   0.93270   0.93270   0.93270

ans(:,:,2) =

   0.65892   0.65892   0.65892
   0.65892   0.65892   0.65892
   0.65892   0.65892   0.65892

ans(:,:,3) =

   0.090389   0.090389   0.090389
   0.090389   0.090389   0.090389
   0.090389   0.090389   0.090389

ans(:,:,4) =

   0.31465   0.31465   0.31465
   0.31465   0.31465   0.31465
   0.31465   0.31465   0.31465

ans(:,:,5) =

   0.50921   0.50921   0.50921
   0.50921   0.50921   0.50921
   0.50921   0.50921   0.50921

I am not sure this is exactly what you are looking for. But the values in E are not represented anywhere, just the dimensions are preserved, and the values of t are repeated along the 3rd dimension of every element in E

Note : If t were a cloumn vector, it has to be permute(t, [2,3,1]) instead.