如何以我想要的方式删除单元格中的空元素 [英] how to delete empty elements in the cell in a way i want

查看:83
本文介绍了如何以我想要的方式删除单元格中的空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我有一个像这样的单元格

in MATLAB I have a cell array like this

a = { 1 2 2 3 4 5 [] []
      2 4 5 4 3 2 4 5 
      4 5 4 3 4 [] [] []}

我想要删除空元素的方式如下:

I want to remove empty elements in a way that I get this :

a = { 1 2 2 3 4 5 2 4 5 4 3 2 4 5 4 5 4 3 4}

但是当我使用这: a(cellfun(@ isempty,a))= [];
我得到的是这个东西:

but when I use this : a(cellfun(@isempty,a)) = []; what I get is this :

a = {1 2 4 2 4 5 2 5 4 3 4 3 4 3 4 5 2 4 5}

这不是我想要的

推荐答案

问题是线性索引沿行方向运行,即它穿过第一个列,然后穿过第二列,依此类推。

The problem is that the linear index runs in the direction of rows, i.e. it runs through the first conlumn, then through the second column etc.

调用在向量上重塑

>> reshape([1 2 3 4 5 6 7 8 9],3,3)
ans =
     1     4     7
     2     5     8
     3     6     9

要获得所需的结果,需要先转置 a

To achieve the result you want, you need to transpose a before indexing into it.

a = a';
a(cellfun(@isempty,a)) = [];

这篇关于如何以我想要的方式删除单元格中的空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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