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
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
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)
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) = {[]}
What about doing this directly:
Result = cellfun(@(c) c(c < 0.1), A, 'UniformOutput', false)
更多回答(1 个)
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]
X = M<0.1 % simple and very efficient, unlike anything with cell arrays
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!