Alon Shmiel
Alon Shmiel

Reputation: 7121

matlab remove NaN from matrix

I have a matrix:

raw = 

'alon'     'tomer'    'ilana'    'T1'     '2'      '53'      '24'       'NaN'
'ilana'    'oren'     '6'        'a'      'g'      'g'       'gsh'      'NaN'
'dikla'    'aba'      'mama'     'a'      'h'      'ghfs'    'bfsbf'    'NaN'
'4'        'NaN'      'NaN'      'nn'    'NaN'    'NaN'     'hadhd'    'NaN'

and I want to remove NaN and to get:

    'alon'     'tomer'    'ilana'    'T1'     '2'      '53'      '24'
    'ilana'    'oren'     '6'        'a'      'g'      'g'       'gsh'
    'dikla'    'aba'      'mama'     'a'      'h'      'ghfs'    'bfsbf'
    '4'         ''        ''         'nn'     ''       ''        'hadhd'

how do I do that?

I tried a lot of suggestions, but I got errors:

1)

>> raw=raw(~isnan(raw(:,2)),:)
??? Undefined function or method 'isnan' for input arguments of type 'cell'.

2)

raw(isnan(raw(:,1)),:) = [];
??? Undefined function or method 'isnan' for input arguments of type 'cell'.

3)

raw(~any(isnan(raw),2),:)
??? Undefined function or method 'isnan' for input arguments of type 'cell'.

4)

T(cellfun(@isnan,T))={0}
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

I tried Ansari's solution, but now I got:

raw = 

'alon'     'tomer'    'ilana'    'T1'    '2'    '53'      '24'       [0]
'ilana'    'oren'     '6'        'a'     'g'    'g'       'gsh'      [0]
'dikla'    'aba'      'mama'     'a'     'h'    'ghfs'    'bfsbf'    [0]
'4'        [    0]    [    0]    'nn'    [0]    [   0]    'hadhd'    [0]

it's not good for me, because I want to do:

size(raw,2) 

and to get:

7

Upvotes: 1

Views: 4162

Answers (4)

Amro
Amro

Reputation: 124553

Another solution:

%# replace all 'NaN' with ''
raw(ismember(raw,'NaN')) = {''};

%# remove rows/columns of all empty strings
raw(:,all(cellfun(@isempty,raw),1)) = [];
raw(all(cellfun(@isempty,raw),2),:) = [];

Upvotes: 1

mwengler
mwengler

Reputation: 2778

You seem to be overworking this.

cooked = regexprep(raw,'NaN','');

does it.


And of course, assuming you are not getting paid an extra $1000 for each line of code you don't write:

cooked = raw;
for I = 1:length(raw(:))
    if strcmp(raw{I},'NaN')
        cooked{I} = '';
    end
end

Upvotes: 1

nrz
nrz

Reputation: 10550

An alternative answer using regexprep:

% the example data:

raw = { 'alon' 'tomer' 'ilana' 'T1' '2' '53' '24' 'NaN'; 'ilana' 'oren' '6' 'a' 'g' 'g' 'gsh' 'NaN'; 'dikla' 'aba' 'mama' 'a' 'h' 'ghfs' 'bfsbf' 'NaN'; '4' 'NaN' 'NaN' 'nn' 'NaN' 'NaN' 'hadhd' 'NaN' }

Here's the code:

output = cellfun(@(x) regexprep(x, 'NaN', ''), raw, 'UniformOutput', false);
NaNraw = cellfun(@(x) strcmp(x, 'NaN'), raw);
output(:,all(NaNraw,1)) = [];

So, first all 'NaN' strings of raw are replaced with '' by using cellfun and repexprep, the result is stored in output. Then a logical matrix NaNraw is created by comparing each string of raw with 'NaN' by using cellfun and strcmp. Finally, the columns of output whose corresponding NaNraw columns contain only ones are removed.

output = 
'alon'     'tomer'    'ilana'    'T1'    '2'    '53'      '24'   
'ilana'    'oren'     '6'        'a'     'g'    'g'       'gsh'  
'dikla'    'aba'      'mama'     'a'     'h'    'ghfs'    'bfsbf'
'4'        ''         ''         'nn'    ''     ''        'hadhd'

Upvotes: 1

Ansari
Ansari

Reputation: 8218

Something like this will work:

f = @(x) strcmp(x, 'NaN');
nans = cellfun(f, raw);
raw(nans) = {''}; %cell(sum(nans(:)), 1);

isnan only works on matrices, so it won't work on cell arrays. Also your NaNs seem to be strings, not real NaNs.

At this point, all the 'NaN's will be empty strings. The following will remove entire rows or columns of empty strings:

raw = raw(:, arrayfun(@(i) length([raw{:, i}]), 1:size(raw, 2)) > 0); % for columns
raw = raw(arrayfun(@(i) length([raw{i, :}]), 1:size(raw, 1)) > 0, :); % for rows

For the last step though you might as well loop to keep the code clean and understandable (and easier to debug!). You're basically collapsing each row or each column by concatenating the strings, then checking if the length is zero, and if it is you're removing that row or column.

Upvotes: 3

Related Questions