how to remove specific rows of columns

2 views (last 30 days)
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 !

Accepted Answer

James Tursa
James Tursa on 27 Feb 2017
Edited: James Tursa on 27 Feb 2017
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)

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!