Remove NaN from the cell array without changing the row and column of matrix
    6 views (last 30 days)
  
       Show older comments
    
Hi all,
I have a question related to how to remove NaN in cell array without changing any rows and column of matrix.
for example,
A = [ NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN,
    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN, 
    NaN NaN NaN NaN 25  NaN NaN NaN NaN NaN, 
    NaN NaN  23  24 25   27  NaN NaN NaN NaN, 
    NaN 22  30  35  23  23  23  23  NaN NaN, 
     24  23  33  23  23 19 20  20  20  21,
    NaN NaN  23  21 28   22  NaN NaN NaN NaN, 
    NaN NaN NaN NaN 25  NaN NaN NaN NaN NaN, 
    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]
I would like to get rid of NaN but keeping the same character of matrix, such as
A = [ _ _ _ _ _ _ _ _ _ _ ,
      _ _ _ _ _ _ _ _ _ _, 
    _ _ _ _ 25 _ _ _ _ _, 
    _ _  23 24 25 27  _ _ _ _, 
    _ 22  30  35  23  23  23  23 _ _, 
     24  23  33  23  23 19 20  20  20  21,
    _ _  23  21 28  22  _ _ _ _, 
   _ _ _ _ 25 _ _ _ _, 
     _ _ _ _ _ _ _ _ _ _ ]
* _ means no any valu, even 0
How could I do that, Thanks very much in advance.
4 Comments
Answers (1)
  Michelle Wu
    
 on 26 Sep 2016
        It is my understanding that you are trying to remove the NaNs from a cell array since they would somehow affect the calculations you are trying to perform. However, the code snippet you provided in your initial description contradicts with your comments of A being a matrix containing cell arrays. I will assume A to be a 9-by-10 cell array according to your example, and hopefully my answer still applies to whatever kind of cell array you are actually using.
Create the 9-by-10 cell array A from an array arr:
   >> arr = [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
          NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
          NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN;
          NaN NaN 23 24 25 27 NaN NaN NaN NaN;
          NaN 22 30 35 23 23 23 23 NaN NaN;
          24 23 33 23 23 19 20 20 20 21;
          NaN NaN 23 21 28 22 NaN NaN NaN NaN;
          NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN;
          NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN];
   >> A = num2cell(arr);
The cell array A would look like:
    A = 
        [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [NaN]    [NaN]    [NaN]    [ 25]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [NaN]    [ 23]    [ 24]    [ 25]    [ 27]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [ 22]    [ 30]    [ 35]    [ 23]    [ 23]    [ 23]    [ 23]    [NaN]    [NaN]
        [ 24]    [ 23]    [ 33]    [ 23]    [ 23]    [ 19]    [ 20]    [ 20]    [ 20]    [ 21]
        [NaN]    [NaN]    [ 23]    [ 21]    [ 28]    [ 22]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [NaN]    [NaN]    [NaN]    [ 25]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]
        [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]    [NaN]
Find the NaNs in A, and replace them with []:
   >> A(cellfun(@isnan,A)) = {[]};
Now A becomes:
    A = 
        []      []      []      []      []      []      []      []      []      []
        []      []      []      []      []      []      []      []      []      []
        []      []      []      []    [25]      []      []      []      []      []
        []      []    [23]    [24]    [25]    [27]      []      []      []      []
        []    [22]    [30]    [35]    [23]    [23]    [23]    [23]      []      []
      [24]    [23]    [33]    [23]    [23]    [19]    [20]    [20]    [20]    [21]
        []      []    [23]    [21]    [28]    [22]      []      []      []      []
        []      []      []      []    [25]      []      []      []      []      []
        []      []      []      []      []      []      []      []      []      []
0 Comments
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


