0

If I have a matrix that is 27 by 12, there are some elements that are empty. like so, [ ]

I am trying to replace whatever elements are [ ] to -1.

What is the best way to do this?

Tofurkey
  • 1
  • 3
  • Is it a matrix or cell array? I've never seen such a matrix and don't tihnk it is possible – Dmitry Galchinsky Mar 14 '13 at 22:27
  • er may possibly be an array then – Tofurkey Mar 14 '13 at 22:28
  • 1
    possible duplicates http://stackoverflow.com/questions/3400515/how-do-i-detect-empty-cells-in-a-cell-array and http://stackoverflow.com/questions/2624016/replace-empty-cells-with-logical-0s-before-cell2mat-in-matlab – gevang Mar 14 '13 at 22:39

1 Answers1

7

I assume that you are talking about a cell array.

In this case, the easiest would be:

%# 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);
Jonas
  • 74,690
  • 10
  • 137
  • 177