Logical indexing cell array

조회 수: 4 (최근 30일)
azarang asadi
azarang asadi 2022년 5월 27일
댓글: azarang asadi 2022년 5월 30일
I have a cell array as follows:
array =
5×4 cell array
{[ 0.5000]} {[ 0.0250]} {[0.4000]} {[ 0.0150]}
{[ 0.0500]} {[ 0.0300]} {[0.0600]} {[ 0.0120]}
{0×0 double} {[ 0.1500]} {[0.0500]} {[ 0.0160]}
{0×0 double} {0×0 double} {[0.0400]} {[ 0.1400]}
{0×0 double} {0×0 double} {[0.0300]} {0×0 double}
what I'm trying to identify the members<0.1 so I'm looking for the following:
array2 =
5×4 cell array
{[ 1]} {[ 0]} {[1]} {[ 0]}
{[ 0]} {[ 0]} {[0]} {[ 0]}
{0×0 double} {[ 1]} {[0]} {[ 0]}
{0×0 double} {0×0 double} {[0]} {[ 1]}
{0×0 double} {0×0 double} {[0]} {0×0 double}
and what I want finally is to get only the members that are <0.1 which is the following:
array3 =
5×4 cell array
{0×0 double} {[ 0.0250]} {0×0 double} {[ 0.0150]}
{[ 0.0500]} {[ 0.0300]} {[ 0.0600]} {[ 0.0120]}
{0×0 double} {0×0 double} {[ 0.0500]} {[ 0.0160]}
{0×0 double} {0×0 double} {[ 0.0400]} {0×0 double}
{0×0 double} {0×0 double} {[ 0.0300]} {0×0 double}
How can I do this? :)
  댓글 수: 1
Image Analyst
Image Analyst 2022년 5월 28일
Why are you using a cell array for this? I don't see a variety of class types, nor do the cells contain variable/different sized arrays. It should be just a regular numerical array, like a double. See the FAQ:

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

채택된 답변

Jan
Jan 2022년 5월 27일
편집: Jan 2022년 5월 27일
A hint: I takes some time to create your input data such, that it can be used in an example. Please do such tedious editing by your own in future questions. Thanks.
A = {0.5000, 0.0250, 0.4000, 0.0150; ...
0.0500, 0.0300, 0.0600, 0.0120; ...
[] , 0.1500, 0.0500, 0.0160; ...
[], [], 0.0400, 0.1400; ...
[], [], 0.0300, []};
T = cellfun(@(c) any(c < 0.1), A)
T = 5×4 logical array
0 1 0 1 1 1 1 1 0 0 1 1 0 0 1 0 0 0 1 0
Do you really need this (called "array2" in your question) as a cell array? Logical indices are fine as logical array.
Result = A;
Result(~T) = {[]}
{0×0 double} {[ 0.0250]} {0×0 double} {[ 0.0150]} {[ 0.0500]} {[ 0.0300]} {[ 0.0600]} {[ 0.0120]} {0×0 double} {0×0 double} {[ 0.0500]} {[ 0.0160]} {0×0 double} {0×0 double} {[ 0.0400]} {0×0 double} {0×0 double} {0×0 double} {[ 0.0300]} {0×0 double}
What about doing this directly:
Result = cellfun(@(c) c(c < 0.1), A, 'UniformOutput', false)
{0×0 double} {[ 0.0250]} {0×0 double} {[ 0.0150]} {[ 0.0500]} {[ 0.0300]} {[ 0.0600]} {[ 0.0120]} {0×0 double} {0×0 double} {[ 0.0500]} {[ 0.0160]} {0×0 double} {0×0 double} {[ 0.0400]} {0×0 double} {0×0 double} {0×0 double} {[ 0.0300]} {0×0 double}

추가 답변 (1개)

Stephen23
Stephen23 2022년 5월 28일
편집: Stephen23 2022년 5월 28일
That is an inefficient way to store and process that data.
Using one numeric array would be simpler and much more efficient:
M = [0.5,0.025,0.4,0.0150;0.05,0.03,0.06,0.0120;NaN,0.15,0.05,0.0160;NaN,NaN,0.04,0.1400;NaN,NaN,0.03,NaN]
M = 5×4
0.5000 0.0250 0.4000 0.0150 0.0500 0.0300 0.0600 0.0120 NaN 0.1500 0.0500 0.0160 NaN NaN 0.0400 0.1400 NaN NaN 0.0300 NaN
X = M<0.1 % simple and very efficient, unlike anything with cell arrays
X = 5×4 logical array
0 1 0 1 1 1 1 1 0 0 1 1 0 0 1 0 0 0 1 0

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by