# help understanding cell and (end+1) use

152 views (last 30 days)
gaetano mallardo on 25 Mar 2019
Answered: Rik on 25 Mar 2019
Hi, i have a script where for the first time i meet the cell array. I've read the documentation and it seems quite clear.
The problem is that i don't understand how they set the indices as they use (end+1). To what it refers as i have 2 for cicle?? If the value is end+1= np.max+1, why don't he just write np+1.
Connectivity=cell(1,nnode);
for i=1:nele
idnodei=element(i,:);
% number of node for element "i-th"
np=length(idnodei);
for j=1:np
nodej=idnodei(j);
Connectivity{nodej}(end+1)=i;
end
end

Rik on 25 Mar 2019
The end keyword (when used in indexing) is converted to the size of that dimension.
%example:
A=rand(1,2,5,3);
A(1,1,end,1)=2;%end is replaced by size(A,3), so with 5
This also works in other calls:
%example:
A=rand(1,2,5,3);
A(1,min(end,7),2,2)=-1;%here end is replaced by the value 2 **before** the call to min
So in your code end+1 means that you are assigning a value to the next position, which automatically extends the array to fit that size.
NB: I disagree with using i and j as variables here, as well as the lack of any comments whatsoever. You see that a lack of comments causes confusion.