Logical indexing cell array

8 views (last 30 days)
azarang asadi
azarang asadi on 27 May 2022
Commented: azarang asadi on 30 May 2022
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 Comment
Image Analyst
Image Analyst on 28 May 2022
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:

Sign in to comment.

Accepted Answer

Jan
Jan on 27 May 2022
Edited: Jan on 27 May 2022
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}

More Answers (1)

Stephen23
Stephen23 on 28 May 2022
Edited: Stephen23 on 28 May 2022
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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!