Effacer les filtres
Effacer les filtres

Remove negative numbers and rows from an array

5 vues (au cours des 30 derniers jours)
Richard Rees
Richard Rees le 18 Juin 2019
Modifié(e) : Adam Danz le 19 Juin 2019
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

Réponse acceptée

Adam Danz
Adam Danz le 18 Juin 2019
Modifié(e) : Adam Danz le 19 Juin 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);

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by