Logical indexing cell array

8 次查看(过去 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