2013-03-14 91 views
0

如果我有一个由12乘以27的矩阵,那么有些元素是空的。像这样,[]用-1替换矩阵中的空元素的最佳方法是什么? MATLAB

我试图取代任何元素[]为-1。

这样做的最好方法是什么?

+0

它是一个矩阵或单元阵列?我从来没有见过这样的矩阵,不要以为它是可能的 – 2013-03-14 22:27:41

+0

呃可能是一个数组,然后 – Tofurkey 2013-03-14 22:28:57

+1

可能的重复http://stackoverflow.com/questions/3400515/how-do-i-detect-empty-cells -in-a-cell-array和http://stackoverflow.com/questions/2624016/replace-empty-cells-with-logical-0s-before-cell2mat-in-matlab – gevang 2013-03-14 22:39:41

回答

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); 
+1

你也可以这样做:'C(cellfun (@ isempty,C))= {-1}'没有'deal'。 – 2013-03-17 14:56:35

+0

@EitanT:确实。感谢您的提醒! – Jonas 2013-03-17 16:19:07

相关问题