Remove negative numbers and rows from an array

조회 수: 12 (최근 30일)
Richard Rees
Richard Rees 2019년 6월 18일
편집: Adam Danz 2019년 6월 19일
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

채택된 답변

Adam Danz
Adam Danz 2019년 6월 18일
편집: Adam Danz 2019년 6월 19일
@ 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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by