Remove negative numbers and rows from an array

10 views (last 30 days)
Good afternoon,
What I want to do is to find and remove all negative values in the 2 column of each matrix within the array and its corrosponding row, leaving only the postive ones. The codes with I have used are unable to achieve this as there are always problems involving the exceedance of array bounds.
Could you help please?
%% Flume find peaks
[n,m] = size(flume);
data_flume = []; % WL is the final column
for i = 2:m %Excludes 1st column
[pks{i}, idx{i}] = findpeaks(flume(:,i));
data_flume{i}(:,1) = flume(idx{i});
data_flume{i}(:,2) = pks{i}(:);1; % Works to identify peaks and their locations
if isempty(data_flume{i});
data_flume{i} = NaN; % Column 26 will be blank as their are no peaks, this is to fill it in.
end
data_flume{i}([data_flume{:,2}]<0,:)=[] % Code will not remove negative values and their rows
end
%% Alterative code to remove negative values
for j = 1 : numel( data_flume )
data_flume_pos = data_flume{j}(:,2) < 0 ;
data_flume{j} = data_flume{j}(~data_flume_pos,:) ; % Or data_flume{j}(data_flume_pos,:) = [] ;
end

Accepted Answer

Adam Danz
Adam Danz on 18 Jun 2019
Edited: Adam Danz on 19 Jun 2019
@ Richard Rees, I didn't realize you had attached data. I see what the problem was: some of your matrices do not have at least 2 columns of data. These two lines below accomplish what you're aiming for and should be added after your loop. If a matrix does not have at least 2 columns it is ignored.
% Determine which matrices have at least 2 columns
goodMatIdx = cellfun(@(x)size(x,2)>1,data_flume);
% Remove rows with neg vals in col 1
data_flume(goodMatIdx) = cellfun(@(x)x(x(:,2)>=0,:),data_flume(goodMatIdx),'UniformOutput',false);

More Answers (0)

Categories

Find more on Cell Arrays 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!