Logical indexing cell array

3 ビュー (過去 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 件のコメント
azarang asadi
azarang asadi 2022 年 5 月 30 日
Thank you so much

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by