Removing rows in a matrix

3 ビュー (過去 30 日間)
Chris Dan
Chris Dan 2020 年 10 月 19 日
コメント済み: Ameer Hamza 2020 年 10 月 20 日
Hello,
I have this matrix, called A matrix. I am attaching the file with the question. Also the picture of some of its data values
I need to remove some of its rows in a loop. I want to keep rows 1,2 then remove 3,4,5,6, keep 7,8,then remove 9,10,11,12, keep 13,14.. like this it goes on.keep 2 remove 4 rows.
Does anyone know?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
Try this
n = size(Identify_3);
idx = (mod((1:n)-1, 6)+1) <= 2;
Identify_3 = Identify_3(idx, :);
  3 件のコメント
Chris Dan
Chris Dan 2020 年 10 月 19 日
編集済み: Chris Dan 2020 年 10 月 19 日
Hi,
Can you also tell about this matrix:
How can i edit your code to keep the rows 1.48,96,144,192 and so on, basically after the first row, we take 48th row and then double 49+48=96 and so on...
Also if I want to change your code for the original matrix, what should I change?
Ameer Hamza
Ameer Hamza 2020 年 10 月 20 日
You can do it like this
D = C([1 48:48:end], :)

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

その他の回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 19 日
編集済み: Bjorn Gustavsson 2020 年 10 月 19 日
If you can determine/calculate all rows you want to remove at once it is best to do the re-sizing of the array at once, instead of row-by-row (if I've understood this right, the reallocation-operation is the main argument to not automatically and incrementally grow arrays, ought to be similar for shrinking.):
idx2remove = [];
for i1 = 1:size(A,1)
if some_conds(A(i1,:))
idx2remove = [idx2remove,i1];
end
end
A(idx2remove,:) = [];
Ah, if you have to remove rows [3,4,5,6]+6*n then something like this should work:
idx2remove = 1:size(A,1);
idx2remove = reshape(idx2remove,6,[]);
idx2remove = idx2remove(3:end,:);
A(idx2remove(:),:) = [];
If this doesn't work out because your A doesn't have a multiple of 6 rows, then you'll have to modify the creation of the remove-array a bit. You could also go for creating an array with row-indices to keep and do:
A = A(idx2keep,:);
HTH
  1 件のコメント
Chris Dan
Chris Dan 2020 年 10 月 19 日
Thanks :)

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


KSSV
KSSV 2020 年 10 月 19 日
Let A be your data. It seems every four rows have real value of complex number very less. We can use keep these rows.
idx = real(abs(A(:,1)))<=10^-10 ; % this will give indices of what you want to keep
iwant = A(idx,:)
  1 件のコメント
Chris Dan
Chris Dan 2020 年 10 月 19 日
Thanks :)

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by