在MATLAB中删除空单元格

在MATLAB中删除空单元格

本文介绍了在MATLAB中删除空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除matlab单元阵列底部的所有空单元.但是,我发现的所有代码示例都将矩阵折叠成一个向量,这不是我想要的.

I want to remove all empty cells at the bottom of a matlab cell array. However all code example that I found collapse the matrix to a vector, which is not what I want.

这段代码

a = { 1, 2; 3, 4; [], []}
emptyCells = cellfun('isempty', a);
a(emptyCells) = []

此向量的结果

但是我想要这个数组

[1]    [2]

[3]    [4]

我该怎么做?

推荐答案

如果要删除单元格数组中所有单元格都为空的所有行,则可以使用以下方法:

If you want to delete all the rows in your cell array where all cell are empty, you can use the follwing:

a = { 1, 2; 3, 4; [], []}
emptyCells = cellfun('isempty', a);

a(all(emptyCells,2),:) = []

a =
    [1]    [2]
    [3]    [4]

它在您的公式中不起作用的原因是,如果您使用数组建立索引,则输出将被整形为向量(因为不能保证将删除整个行或列,而不能保证只是删除某个位置的单个元素).

The reason it didn't work in your formulation is that if you index with an array, the output is reshaped to a vector (since there is no guarantee that entire rows or columns will be removed, and not simply individual elements somewhere).

这篇关于在MATLAB中删除空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:20