how to remove specific rows of columns

2 ビュー (過去 30 日間)
farfar
farfar 2017 年 2 月 27 日
コメント済み: farfar 2017 年 2 月 27 日
Hey everyone. I have one matrix [a] with 5 columns. i am removing some rows of b=a(:,4) based on some threshold, and then I need to construct a new matrix with the new column 4 . but I also need to include the first three columns. how can I remove the same rows from the first three columns? Thanks !

採用された回答

James Tursa
James Tursa 2017 年 2 月 27 日
編集済み: James Tursa 2017 年 2 月 27 日
Save the indexing that you use to do the removing, and then apply that to the original matrix during the reconstruction. E.g.,
>> a = reshape(1:30,6,5)
a =
1 7 13 19 25
2 8 14 20 26
3 9 15 21 27
4 10 16 22 28
5 11 17 23 29
6 12 18 24 30
>> b = a(:,4)
b =
19
20
21
22
23
24
>> rows_to_remove = mod(b,2)==1
rows_to_remove =
1
0
1
0
1
0
>> result = [a(~rows_to_remove,1:3) b(~rows_to_remove)]
result =
2 8 14 20
4 10 16 22
6 12 18 24
Or suppose instead of logical indexing as in the above example, you had row numbers to remove. E.g.,
rows_to_remove = [1 3 5]
Then just transform this into a logical indexing vector first. E.g.,
rows_to_remove = ismember(1:size(a,1),rows_to_remove)
  1 件のコメント
farfar
farfar 2017 年 2 月 27 日
Thank you !

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by