why it will go wrong using 'cell'?

조회 수: 2 (최근 30일)
warnerchang
warnerchang 2022년 3월 31일
댓글: warnerchang 2022년 4월 1일
Recently, I ran two codes as follow:
a={[],[]};a{1}(a{1}==3)=[];
% it will go wrong
b=cell(1,2);b{1}(b{1}==3)=[];
  댓글 수: 2
KSSV
KSSV 2022년 3월 31일
The difference is the space allocated for each.
a={[],[]};
b=cell(1,2);
whos a b
Name Size Bytes Class Attributes a 1x2 208 cell b 1x2 16 cell
warnerchang
warnerchang 2022년 3월 31일
however,what happened behind cell function? As far as I know, a = {[],[]} and b = cell(1,2) should get the same result.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2022년 3월 31일
편집: Jan 2022년 3월 31일
I confirm, that this is an inconsistent behavior.
a = {[], []};
b = cell(1, 2);
isequal(a, b)
ans = logical
1
a{1}([]) = [] % Working: Nothing is deleted as expected
a = 1×2 cell array
{0×0 double} {0×0 double}
b{1}([]) = [] % Failing:
Deletion requires an existing variable.
Both methods create a cell array of the same size, but cell() fills the elements with a NULL pointers, while {[], []} creates empty matrices. Matlab should treat NULL pointers as empty matrices, but as you see in this example, this exception has been forgotten sometimes. You cannot detect the NULL points from inside Matlab, but on the C level this is easy. KSSV's method to check the used memory is a strong hint also.
Report this bug to MathWorks. The function for deleting will be updated than to catch this exception.

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by