How to select data from a cell array based on some condition
15 次查看(过去 30 天)
显示 更早的评论
Hi, I am facing a problem with cell array, I have a data, for example, A as below
A = {[0, 5], [-2, 3]; [0,0], [12,-21]};
And I want the output as a cell/array of values greater than 2, like this-
B = {[5], [3]; [0], [12]}
I tried this way but its not working
K = @(data, fn) cellfun(@(x) x(data{:}), fn,'UniformOutput',0);
B = K(A, {@(x) x > 2})
I would appreciate if anybody help me with this
2 个评论
Stephen23
2019-9-1
"I want the output as a cell/array of values greater than 2, , like this- B = {[5], [3]; [0], [12]}"
How is zero greater than two ?
采纳的回答
Stephen23
2019-9-2
>> A = {[0,5], [-2,3]; [0,0], [12,-21]};
>> B = cellfun(@(v)v(v>2),A,'Uni',0);
>> B(cellfun('isempty',B)) = {NaN}
B =
[ 5] [ 3]
[NaN] [12]
更多回答(1 个)
Walter Roberson
2019-8-31
B = cellfun(@(data) max(data), A, 'uniform', 0);
C = cellfun(@(data) min(data), A, 'uniform', 0);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!