help understanding cell and (end+1) use

152 views (last 30 days)
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.
for i=1:nele
% number of node for element "i-th"
for j=1:np

Accepted Answer

Rik on 25 Mar 2019
The end keyword (when used in indexing) is converted to the size of that dimension.
A(1,1,end,1)=2;%end is replaced by size(A,3), so with 5
This also works in other calls:
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.

More Answers (0)


Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!