用-1替换矩阵中的空元素的最佳方法是什么? MATLAB

时间:2013-03-14 22:24:15

标签: matlab replace

如果我的矩阵是27乘12,那么有些元素是空的。像这样,[]

我正在尝试将[]的所有元素替换为-1。

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:7)

我假设你在谈论一个单元阵列。

在这种情况下,最简单的是:

%# create some sample data
C = {1,2,[];3,[],99};

%# replace empty elements with -1
[C{cellfun(@isempty,C)}] = deal(-1);

%# or, simpler (thanks @EitanT)
C(cellfun(@isempty,C)) = {-1};


%# just in case you want to turn C into a numeric array
numericC = cell2mat(C);