In Matlab, I was trying to put anonymous functions in an array:
>> a=[@(k)0.1/(k+1) @(k)0.1/(k+1)^0.501]
??? Error using ==> horzcat
Nonscalar arrays of function handles are not allowed; use cell arrays
instead.
So I wonder what kinds of elements are allowed in an array, and in a cell array?
For example, I know that in an array, the elements can be numerical or strings. What else?
In short: Cell array is a heterogeneous container, regular array is homogeneous. This means that in a regular array all of the elements are of the same type, whereas in cell array, they can be different. You can read more about cell array here.
Use cell array when:
Prefer regular array when:
More explanation about copy-on-write:
When you pass an array to a function, a pointer/reference is passed.
But when you change it (or a part of it), a copy is created.
In a cell array, only the cell is copied.
Thus, if you call a function that changes a single element on a large array, you are making a lot of copies – that makes it slower. But in a cell array, it is not the case.