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

0 matlab replace

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

我试图将[]的任何元素替换为-1.

做这个的最好方式是什么?

Jon*_*nas 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);
Run Code Online (Sandbox Code Playgroud)