如何找到最后一个非空元素的索引单元阵列 [英] How to find index of the last non-empty element in a cell array

查看:428
本文介绍了如何找到最后一个非空元素的索引单元阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我初始化了一个很长的电池阵列(矢量?)

I initialized a very long cell array(vector?)

train_labels = cell(16218, 1);

这将使用来自50个不同的文件夹中的文件,以确保文件是在正确的位置索引,我需要被写入到最后一个单元格数组的索引进行填充。

These will be populated using files from 50 different folders, to make sure that the files are indexed in the right location, I need the index of the last cell array that was written to.

例如读取一个文件夹,在 train_labels 该指数曾达到后 5406 。我们了解,他们必须保存到是下一个索引下一个文件夹读取图像 5407 。为了使这项工作,我需要找到的最后一个非空数组的位置 train_labels

For example after reading one folder, the index in train_labels had reached 5406. Now to read the images from the next folder they must be saved to the next index that is 5407. To make that work I need to find the location of the last non-empty array in train_labels.

由于简单的找到(train_labels,1,'最后')不会对电池阵列工作。我用 cellfun 但是这也不能正常工作。这是我尝试:

Since the simple find(train_labels,1,'last') does not work on cell arrays. I used cellfun but that is also not working. This is what I tried:

cellfun(@find, train_labels, 'last')
Error using cellfun
Input #3 expected to be a cell array, was char instead.

请问AP preciate如何获取单元阵列的最后一个索引任何指导。

Would appreciate any guidance on how to get the last index of the cell array.

感谢您

推荐答案

使用 cellfun '的isEmpty 选项 -

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

您也可以使用 cellfun(@isempty ..),但我相信一定是慢。这较详细的无证MATLAB博客帖子进行了讨论。

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 是内置的,因此似乎是一个优化的实现。其他内置插件,在 cellfun 的2014A版本都可以是 - 伊斯雷尔 ISLOGICAL长度为ndims 'prodofsize '大小''isclass 。我希望,这些都是快速的实现也是如此。这些详细信息都在其官方文档中有可与&GT进行访问;>帮助cellfun

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.

这篇关于如何找到最后一个非空元素的索引单元阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆