필터 지우기
필터 지우기

cell array to logical

조회 수: 70 (최근 30일)
Ioannis Vourvachakis
Ioannis Vourvachakis 2021년 10월 11일
편집: DGM 2021년 10월 11일
I have this cell array and i want to convert this to logical array.
Thank you
  댓글 수: 1
stozaki
stozaki 2021년 10월 11일
Do you treat null values (empty: []) as "false"?

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

답변 (1개)

DGM
DGM 2021년 10월 11일
편집: DGM 2021년 10월 11일
In order to make a logical array of it, you'll have to specify the conditions where the output is true. Consider two examples:
A = {1; 1; 1; []; 1; 1; []; 0; 0.1}
A = 9×1 cell array
{[ 1]} {[ 1]} {[ 1]} {0×0 double} {[ 1]} {[ 1]} {0×0 double} {[ 0]} {[ 0.1000]}
% true if non-empty
B = ~cellfun(@isempty,A)
B = 9×1 logical array
1 1 1 0 1 1 0 1 1
% true if nonzero
C = cellfun(@(x) x~=0,A,'uniform',false)
C = 9×1 cell array
{[ 1]} {[ 1]} {[ 1]} {0×0 logical} {[ 1]} {[ 1]} {0×0 logical} {[ 0]} {[ 1]}
% true if non-empty and nonzero
D = cellfun(@(x) ~isempty(x) && x~=0,A)
D = 9×1 logical array
1 1 1 0 1 1 0 0 1
Note that the second example is still a cell array. This is required if empty elements are to be preserved,.
  댓글 수: 2
Jan
Jan 2021년 10월 11일
When the elements of the cell array are numerical arrays, this is faster:
B = ~cellfun('isempty', A)
Ioannis Vourvachakis
Ioannis Vourvachakis 2021년 10월 11일
Thank you very much!!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by