Mixo
Mixo

Reputation: 191

Find negative values in a cell array

I have the following cell of data

lines{1}

ans =

'1000.0    -7    NaN    NaN    70'     
'995.0     32   11.0    6.9    -7'    
'962.0    313    8.8    6.2    84'  
'925.0    640    6.2    5.5    95'  

I would test if the columns number 2,3,5 have negative values and then delete the relative rows. In this case the result will be:

962.0    313    8.8    6.2     84  
925.0    640    6.2    5.5     95  

I updated my post because I deleted some rows and then I could convert to a numerical matrix but I receive the message: "Error using cat Dimensions of matrices being concatenated are not consistent."

Upvotes: 1

Views: 1395

Answers (3)

Ander Biguri
Ander Biguri

Reputation: 35525

My guess is that you want to iterate in that cell, and probably what people is suggeting you ( create a normal array) is the best way to approach. But if you really need to do it this way, here is a code that will do the work:

clear; clc;

lines{1}=['1000.0    -7    NaN    NaN    70',  
'995.0     32   11.0    6.9    -7',    
'962.0    313    8.8    6.2    84',  
'925.0    640    6.2    5.5    95' ]


cols=[2,3,5];                        % Colums to check for negatives

auxdata=str2num(lines{1});
del=sum((auxdata(:,cols)<0)');
indx=1:size(lines{1},1);
del=del.*(1:size(lines{1},1));
del(~del)=[];
lines{1}(del,:)=[];
data=str2num(lines{1})              % non-deleted numerical data

With this code youll have the result in double in the variable data and the string rows deleted from the cell.

This code is easily converted to a for for different lines{ii}

Upvotes: 1

Nishant
Nishant

Reputation: 2619

I am assuming that you have a cell array lines of matrices.

column_ = [2 3 5];
out1 = cellfun(@(a)({a((~any(a(:,column_)<0,2)),:)}),lines);

out1 will be a cell array consisting of the matrices that you want to the corresponding matrices of lines.

Upvotes: 1

Dan
Dan

Reputation: 45762

M = cell2mat(C)
M(~any(M(:,[2,3,5])<0,2),:)

Upvotes: 3

Related Questions