フィルターのクリア

delete certain elements in a matrix

1 回表示 (過去 30 日間)
dav
dav 2013 年 3 月 14 日
Is there any way to delete certain rows(with a pattern) in a matrix
like to delete rows 1,6,11,21,26,..... using a loop
Thanks.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 14 日
編集済み: Azzi Abdelmalek 2013 年 3 月 14 日
A=rand(40,4)
v=[1 6 11 21 26]
w=sort(v);
for k=0:numel(v)-1
A(w(k+1)-k,:)=[]
end
  7 件のコメント
Jan
Jan 2013 年 3 月 14 日
@dav: Simply set a breakpoint in the code an let it run. In the first iteration the first row is deleted. In the 2nd iteration, you should not delete the 6th row, because in the first iteration the 6th row became the 5th one. Therefore (k-1) is subtracted from the index in the k.th iteration.
Nevertheless, a loop is an inefficient approach and suffers from the old Schlemiel the painter problem.
dav
dav 2013 年 3 月 14 日
Thanks a lot

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

その他の回答 (2 件)

Sven
Sven 2013 年 3 月 14 日
編集済み: Sven 2013 年 3 月 14 日
In a loop? Not really.
All at once? Absolutely.
DATA = rand(50,5);
rowsToDelete = [1,6,11,21,26];
DATA(rowsToDelete,:) = [];
If you mean that your rows to delete has the pattern of rows 1,6,11,16, etc, then just do this:
numRows = size(DATA,1);
rowsToDelete = [1:10:numRows 6:10:numRows];
DATA(rowsToDelete,:) = [];
  3 件のコメント
Jan
Jan 2013 年 3 月 14 日
This is much faster than a loop for large arrays. Shrinking arrays in a loop has the same drawback as letting them grow: In each iteration the new array must be allocated and the contents of the old one is copied. This is very expensive.
dav
dav 2013 年 3 月 14 日
Thanks a lot

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


Jan
Jan 2013 年 3 月 14 日
Alternative to Sven's approach:
Data = rand(50,5);
index = false(1, 50);
index(1:10:end) = true;
index(6:10:end) = true;
Data(index, :) = [];
  1 件のコメント
dav
dav 2013 年 3 月 14 日
Thanks.

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

カテゴリ

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