Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
A declaration like that defines the variable as a) existing, and b) being empty.
(Disappointingly, the following lame example of bad programming practice is the best that springs to mind
Let's say you have a loop in which a variable dynamically grows with each iteration*. For example:
for i=1:10
a = [a i^2];
end
(This will build a row vector of squares).
If you try to run this as is, MATLAB will fail with a warning
??? Undefined function or variable 'a'.
Preallocating a = []; will fix the issue.
MATLAB's help also offers a few more useful applications of the [] assignment:
"A = [ ] stores an empty matrix in A. A(m, = [ ] deletes row m of A. A,n) = [ ] deletes column n of A. A = [ ] reshapes A into a column vector and deletes the nth element."
* MATLAB's M-lint tool will warn you about this sort of code being slow, as the array 'a' must be resized with each loop iteration.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.