[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: 2D matrixes into a 3D matrix

**From**: |
David Bateman |

**Subject**: |
Re: 2D matrixes into a 3D matrix |

**Date**: |
Mon, 29 Jan 2007 21:39:36 +0100 |

**User-agent**: |
Thunderbird 1.5.0.7 (X11/20060921) |

Andres Sepulveda wrote:
>* Hi,*
>* *
>* I want to "stick" a series of 2D (e.g. 2x2) matrixes into a 3D matrix *
>* (2x2xN) with N increasing as I stick a new matrix.*
>* *
>* Is it better to define a large-enough 3D matrix and then reduce its size?*
>* *
>* Andres*
>*> *
I have no idea what you mean from the above statement. If you know the
final size of the 3D matrix, then yes it is better to preallocate the
memory something like
A = zeros([2,2,N]);
for i=1:N
A(:,:,i)= randn(2,2);
endfor
is definitely an advantage, though the above example is artificial and
should be written as A = randn([2,2,2]) instead. Note that is you can
vector the code as do without the loop to create the matrix, you are
much better off without it...
D.