필터 지우기
필터 지우기

help understanding cell and (end+1) use

조회 수: 97 (최근 30일)
gaetano mallardo
gaetano mallardo 2019년 3월 25일
답변: Rik 2019년 3월 25일
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
Rik 2019년 3월 25일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by