How can I delete certain rows of a matrix based on the first time one column is different from zero

1 次查看(过去 30 天)
I have a matrix that has 13 columns and millions of rows.
I need to delete the rows between 2 rows of specific values on different columns : the first one is when the value of column 1 > 600 and the second row is the first time column 8 is equal to 1 (but not every time it is),
Does anybody have an idea on how to manage that?

采纳的回答

Voss
Voss 2022-3-4
When you say "the first time column 8 is equal to 1", do you mean "the first time that column 8 is equal to 1 after the first time column 1 is > 600" (i.e., the end row must come after the start row)?
% remove rows starting with the first row where column 1 is > 600 and
% ending with the first row where column 8 is equal to 1, regardless of
% where they are in relation to each other:
data = randn(100,13);
% start row is 11:
data(11,1) = 601;
% end row is 25:
data(25,8) = 1;
start_idx = find(data(:,1) > 600,1);
end_idx = find(data(:,8) == 1,1);
if ~isempty(start_idx) && ~isempty(end_idx)
data(start_idx:end_idx,:) = []; % remove the rows
end
start_idx
start_idx = 11
end_idx
end_idx = 25
% remove rows starting with the first row where column 1 is > 600 and
% ending with the first row after that where column 8 is equal to 1, if it
% ever is:
data = randn(100,13);
% start row is 11:
data(11,1) = 601;
% now column 8 is equal to 1 two times, once before the "start row" and
% once after (this method will use the one after (25), but the method
% above would use the first one (10) in this case):
data([10 25],8) = 1;
start_idx = find(data(:,1) > 600,1);
if ~isempty(start_idx)
end_idx = start_idx + find(data(start_idx+1:end,8) == 1,1);
if ~isempty(end_idx)
data(start_idx:end_idx,:) = []; % remove the rows
end
end
start_idx
start_idx = 11
end_idx
end_idx = 25

更多回答(1 个)

Arif Hoq
Arif Hoq 2022-3-4
编辑:Arif Hoq 2022-3-4
try this:
M=randi([1 700],10,13); % generating random matrix with 10 rows and 13 column
M(:,8)=[2;5;7;1;6;7;1;4;6;1]; % forcely made some elements of column 8=1;
M(:,1)=[300;400;277;647;500;345;900;786;123;234]; % forcely made some elements of column 8>600
[idx]=find(M(:,1)>600);
M(idx,:)=[]; % delete the rows in which column 1 >600
[idx2]=find(M(:,8)==1);
M(idx2,:)=[] % delete the rows in which column 8 =1

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by