How to find index of the last non-empty element in a cell array

為{幸葍}努か 提交于 2019-12-01 19:50:35

Use cellfun with 'isempty' option -

last_non_empty_index = find(~cellfun('isempty',train_labels),1,'last')

You can also use cellfun(@isempty..), but I believe that must be slower. This has been discussed in detail in this Undocumented MATLAB Blog post.

isempty is a built-in and as such appears to be an optimized implementation. Other built-ins that are available in 2014A version of cellfun are - 'isreal', 'islogical', 'length', 'ndims', 'prodofsize', 'size', 'isclass'. I am hoping that these are fast implementations as well. More info on these are available in its official documentation that can be accessed with >> help cellfun.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!