Cells
Cells – matrix of nsp objects data type
Calling sequence
A={a11, a12, ..., a1n;
a21, a22, ..., a2n;
...
am1, am2, ...; amn}
|
Parameters
Description
The Cells data type is used to create matrix of nsp objects. In the current implementation, cell
arrays are two dimensional arrays.
column cell vectors are considered as m x 1 matrices and row cell vectors as 1 x n
matrices.
Internally Cells type are stored by default as a unidimensional array of Objects, It is therefore
always possible to access elements with one indice assuming a column order storage.
Operations on Cells
Cells methods
- [bool,i,j]=C.has[Obj] returns true if Obj is present in the cell array C and if requested
also returns indices. Presence is checked via the equal operator i.e true is returned if
C{i,j}.equal[Obj].
Methods from the matint interface
- A.redim[m,n] reshape matrix to size mxn. m or n can be set to -1
- A.concatr[B] A = [A,B]
- A.concatd[B] A = [A;B]
- A.perm_elem[p,q,dim] permute values, rows (dim=1) or columns (dim=2).
Extraction, insertion, deletion
For Cells data type, operations performed with {.} operate on
the stored objects whereas operation performes with (.) operate on Cells data type. For example, if
C={5,6}, C(1,1) will return a 1x1 Cells whereas C{1,1} will return the numeric matrix
5.
In the sequel, I and J are matrices giving indices. Thus, I and J are numeric or boolean matrices
the can also be set to : in that case they stand for the whole element, or row ,or column indices of
the matrix.
- A(I,J) is a new cell array build from A with entries in I and J.
- A(I,:) is a new cell array build from A with row indices in I.
- A(:,J) is a new cell array build from A with comumn indices in I.
- A(:,:) is A
- A(:) is a new cell array obtained by stacking the columns of A.
- Note that a unique indice vector can be used using the column order storage.
- A(I,J)=B is only valid if B is of Cells type. it inserts cell array B into cell array
A in rows I and columns J. The dimensions of B must match with dimensions of a
length(I)xlength(J) cell array (which means that B can be a 1x1 cell array or be of
dimension length(I)xlength(J).
- A special case appears when B is an empty cell array. In that case the sub cell array of
A specified by IxJ is removed and the matrix is reshaped to a column array when the
deletion operation is not consistent with original dimension. For example A(:,J)={}
removes the columns of A with indices in J. A(3,3)={} removes element (3,3) and
reshape the array to a column array.
- A{I,J}=... fills the entries of cell array A in rows I and columns J with the right hand
side values of the affectation sign. The values are of course stored using the column order
storage. The size of the cell array A can grow according to rows and columns indices.
For example if A={1,5} and we use A{2:3,1:2}=(1,2,3,4) then A will be a 3x2 cell
array.
- A{I,J} returns length(I)xlength(J) objects on the calling stack extracted from A. For
example if A={[1,2],[5;6]} then [x,y]=A{1:2} will store [1,2] in x and [5;6] in y.
Insertionand extraction can of course be recursively called on the cell array elements
by chaining operator {.} or (.) as for example in A={4,5};A{2}(3,4)=45.
Empty cells
Empty cells are Cells with zero rows or zero columns. Operations with empty cells
are compatible with linear algebra operations.
For loop control
Let A be a Cells object :
is a loop with size(A,2) iterations, the loop variable col being set to the ith column of A at the i-th
iteration.
Some functions
- unique(C) eliminate redundencies (in the equal sence) in cell array elements.
- ce2m(C,indice=i,notm=val1,noti=val2) converts a cell array to numeric matrix with
the following conventions if a C entry is not a numeric matrix then val1 is inserted
(default value %nan). If C entry is a numeric matrix then entry i of the matrix is
extracted (default value 1) and if indice i does not exists then val2 is inserted.
- map(C,f [,args]) maps the function f to each element of the cell array C returning a
new cell array.
Examples
- Creation, extraction, insertion with {.}:
M=rand(4,4);
A={8,9,M,"nsp"};
b=A{1}
[a,b]=A{[1,3]};
A{1:2}=(M,78) ;
A{:} = (4,5,6,7);
A={8,9;M,"nsp"};
A{1:2,1}=(56,67);
A{1:2,1:2}=(1,2,3,4);
D{3}=8;
- Using {.} to provide a sequence of aguments to a function
function y=f(varargin);y=length(varargin);endfunction;
A={8,9,M,"nsp"}; // creation
f(A{:})
- Creation, extraction, insertion with (.):
A={};
A([1,3]) = {7,8} // affectation
A([1:3]) // extraction of a sub cell array
A(1,2) = {89}; // same result as A{1,2} = 89; A={1,2;3,4}
{ A{:,1}} // 1x2
A(:,1) // 2x1
- using map:
A={7,8,rand(4,6),'foo'};
function y=len(x);y=length(x);endfunction;
Res=map(A,len);
function y=plus_val(x,args);y=x+args(1);endfunction;
Res=map({7,8,rand(2,2)},plus_val,list(45))
See also
Authors jpc