Delete every n rows if less than x

1 回表示 (過去 30 日間)
David du Preez
David du Preez 2017 年 6 月 2 日
回答済み: Anushi1998 2017 年 6 月 2 日
Hi I have a m x 14 matrix. I want to consider the first 24 rows. If the value in coulmn 5 in row 14 is less than x, then delete the first 24 rows. I want to repeat this for the rest of the matrix as well, considering 24 rows at a time
  4 件のコメント
Jan
Jan 2017 年 6 月 2 日
@David: Please give us any useful details. Did you read the "Getting Started" chapters of the documentation already? Did you see Matlab's Onramp tutorials? Is m a multiple of 24? Do you know how to create a FOR loop?
David du Preez
David du Preez 2017 年 6 月 2 日
@Jan: Yes m is a multiple of 24. The matrix contains hourly values. Column 1 contains datenum values. Therefore row 14 column 5 will be the daily 13:00 value. Will have a look at the tutorial now

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 6 月 2 日
編集済み: Jan 2017 年 6 月 2 日
Assuming that m is a multiple of 24:
DataM = reshape(Data, 24, [], 14);
Match = (DataM(14, :, 5) >= x);
Result = reshape(DataM(:, Match, :), [], 14);
For teaching with a FOR loop:
Keep = true(1, size(Data, 1));
for k = 1:24:size(Data, 1)
Keep(k:k+23) = (Data(k+4, 14) >= x);
end
Result = Data(Keep, :);

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 2 日
Let A - your array [m x 14]
ii = ceil((1:m)'/24);
l0 = accumarray(ii,A(:,5) >= x,[],@all);
out = A(l0(ii),:);

Anushi1998
Anushi1998 2017 年 6 月 2 日
Consider going from the end because this won't run your loop infinitely when the portion is not deleted.
Since we have to consider only segments of size 24*14 it won't affect whether we go from top to bottom or bottom to top.
a=%array of size m * 14
for ii=m:-24:24
b=a(ii-23:ii,:);
if(b(14,5)<x)
a(ii-23:ii,:)=[];
end
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by