Remove negative numbers and rows from an array
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!