如果我的矩阵是27乘12,那么有些元素是空的。像这样,[]
我正在尝试将[]的所有元素替换为-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);