如何在MATLAB中删除单元格数组中的零条目? [英] How to remove zero entries inside a cell array in MATLAB?

查看:365
本文介绍了如何在MATLAB中删除单元格数组中的零条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个单元格数组,可以说cell_arr,它具有零个条目以及非零个单元格条目.例如:

I have a cell array in MATLAB, lets say cell_arr and it has zero entries as well as non-zeros cell entries. For example:

cell_arr = {0, 0, 0, 0, 0, {1x3 cell}, {1x3 cell}, {1x3 cell}, {1x3 cell}};

有人可以告诉我如何从cell_arr中删除这些零条目,或者查找非零条目的索引吗?另外,我想避免执行此作业的for循环.

Can somebody please tell how to remove these zero entries from the cell_arr or, to find the indices of non-zero entries? Additionally, I want to avoid for loop for performing this job.

我已经尝试过find函数,但是find函数不适用于单元阵列.我想知道是否存在单行语句/表达式来完成这项工作?

I already tried find function, however, find function is not applicable for cell arrays. I am wondering if there exists a single line statement/expression doing this job?

推荐答案

据我所知,没有单行功能.您必须结合一些功能. 第一行在您的单元格数组中找到零,而第二行则删除这些条目.请注意()括号i.s.o. {}删除.

As far as I know there is no single line function. You have to combine some functionallity. The first line finds the zeros in your cell array, while the second line deletes those entries. Note the () parentheses i.s.o. {} for removal.

尝试一下:

idxZeros = cellfun(@(c)(isequal(c,0)), cell_arr);
cell_arr(idxZeros) = [];

这篇关于如何在MATLAB中删除单元格数组中的零条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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