Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use array and when to use cell array?

Tags:

arrays

matlab

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?

like image 330
Tim Avatar asked Mar 13 '12 12:03

Tim


People also ask

What is the difference between cell and array?

Array = a single variable (of any data type) that contains multiple content elements. Cell array = a specific type of array in MATLAB; an array of class cell. This is the "everything" container in MATLAB -- it's essentially a meta data type, or a "container" data type. You can put anything inside a cell.

What are the advantages of cell array over normal array in MATLAB?

Cell arrays contain data in cells that you access by numeric indexing. Common applications of cell arrays include storing separate pieces of text and storing heterogeneous data from spreadsheets. For example, store temperature data for three cities over time in a cell array. Plot the temperatures for each city by date.

What is a cell array?

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text, combinations of text and numbers, or numeric arrays of different sizes. Refer to sets of cells by enclosing indices in smooth parentheses, () .

How do you use a cell array in MATLAB?

There are two ways to refer to the elements of a cell array. Enclose indices in smooth parentheses, () , to refer to sets of cells — for example, to define a subset of the array. Enclose indices in curly braces, {} , to refer to the text, numbers, or other data within individual cells.


2 Answers

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:

  • You have different types in your array
  • You are not sure whether in the future you might extend it to another types
  • You are working with objects that have an inheritance pattern
  • You are working with an array of strings - almost in any occasion it is preferable to char(n,m)
  • You have a large array, and you often update a single element in a function - Due to Matlabs copy-on-write policy
  • You are working with function handles (as @Pursuit explained)

Prefer regular array when:

  • All of the elements have the same type
  • You are updating the whole array in one shot - like mathematical operations.
  • You want to have type safety
  • You will not change the data type of the array in the future
  • You are working with mathematical matrices.
  • You are working with objects that have no inheritance

More explanation about copy-on-write:

When you pass an array to a function, a pointer/reference is passed.

function foo(x)
     disp(x);
end

x= [1 2 3 4 5];
foo(x); %No copy is done here! A pointer is passed.

But when you change it (or a part of it), a copy is created.

function foo(x)
    x(4) = x(4) + 1;
end

x= [1 2 3 4 5];
foo(x); %x is being copied! At least twice memory amount is needed.

In a cell array, only the cell is copied.

function foo(x)
   x{4} = x{4} + 1;
end

x= {1 2 3 4 5}; %Only x{4} will be 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.

like image 197
Andrey Rubshtein Avatar answered Sep 27 '22 19:09

Andrey Rubshtein


Function handles are actually the exception here, and the reason is that the Matlab syntax becomes surprising if you allow function handles to be a part of non-cell array. For example

a = @(x)x+1;
a(2);  %This returns 2

But, if arrays of function handles were supported, then

b = [@(x)x+1,  @(x)x+2];
b(2);                  %This would return @(x)x+2
b(3) = @(x)x+3;        %This would extend the size of the array

So then would this be allowed?

a(2) = @(x)x+2;       %Would this extend the size of the previously scalar array

Longwinded edit: This is documented in the release notes accompanying release R14, which was the first release allowing anonymous functions. Prior to R14 you could create function handles as references to m-file functions, and they could be placed in non-cell arrays. These could only be called using feval (e.g.: fnSin = @sin; output = feval(fnSin, pi)).

When anonymous functions were introduced, the Mathworks updated the syntax to allow a simpler calling convention (e.g. fnSin = @sin; output = fnSin(pi)) which had the effect of causing an ambiguity when using non-cell array of function handles. It looks like they did their best to grandfather this new behavior in, but those grandfathered conditions have certainly expired (this was 2004).

like image 37
Pursuit Avatar answered Sep 27 '22 20:09

Pursuit