Remove NaN from doubles within a cell array
显示 更早的评论
For example, how would I get this:
ab = {[1 2 NaN 3 4];[1 3 4 NaN 5 6 7 NaN]}
to output
ab = {[1 2 3 4];[1 3 4 5 6 7]}
I've tried
ab(cellfun(@(x) all(isnan(x)),ab)) = []
but it doesn't work
1 个评论
Pruthvi G
2020-3-12
ab(cellfun(@(cell) any(isnan(cell(:))),ab))={''};
采纳的回答
更多回答(3 个)
Geoff Hayes
2017-7-7
Jonathan - remember that the output of cellfun will be a cell array, so the code that you have above will work i.e. using a cell array as the indices into ab. Try defining a function that will do this for you, creating a function named removeNaNs and saving it to a file named removeNaNs.m
function [ab] = removeNaNs(ab)
ab = cellfun(@(x)removeNaNsPriv(x), ab, 'UniformOutput', false);
function [z] = removeNaNsPriv(z)
z(isnan(z)) = [];
We use the second function (saved within the removeNaNs.m file) as a "helper" function to remove the NaNs from an array. From the command line, you would call this code as
>> ab = {[1 2 NaN 3 4];[1 3 4 NaN 5 6 7 NaN]};
>> removeNaNs(ab)
Walter Roberson
2017-7-7
编辑:Walter Roberson
2017-7-7
result = cellfun(@(M) M(~isnan(M)), ab, 'Uniform', 0)
Pruthvi G
2020-3-12
ab(cellfun(@(cell) any(isnan(cell(:))),ab))={''};
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!