How can I delete the rows of a matrix in matlab under a given condition

Hello everybody,
I have a matrix which has 10118 rows and 14 columns. I would like to delete the number of rows having values greater than 100. How can I do this?
Thanks in advance!

 Accepted Answer

EDITED
%Numbers greater than 100
idx = matrix > 100;
matrix ( ~ ( sum ( idx , 2) == 0 ) , : ) = []
%__________________________
%Numbers lesser than 100
idx = matrix < 100;
matrix ( sum ( idx , 2 ) ~= 0 , : ) = []

21 Comments

Thanks for your reply. Could you please let me know what is 'idx'?
idx is the logical index of the values which are greater than 100 and in the next step you put them as row index to remove the rows , its as simple as that
Thanks for your kind reply.
If i run your code, it's only showing '0' where the condition does not satisfy. But i need to delete the entire row if there is at least one value in the row greater than 100.
Say, i have a matrix:
[0 0 0 16.9 0 16.9 0 0 0 0 -16.9 0 0 0
338029.5845 338029.5845 338029.5845 338029.5845 0 0 0 1 0 0 0 16.9 0 0
0 33.799 0 33.799 0 33.799 0 33.799 0 0 0 0 -33.799 0]
Look, in the 2nd row, i have values greater than 100. so i want to delete the entire row. 1st and 3rd row will be remain as there is no value greater than 100.
In this way, i will have to delete all the entire rows which have one or more values greater than 100 among 10118 rows. And finally will get 17 or 18 rows (probably) which do not have any value greater than 100 .
Could you please help me in this regard?
>> matrix=[0 0 0 16.9 0 16.9 0 0 0 0 -16.9 0 0 0;
338029.5845 338029.5845 338029.5845 338029.5845 0 0 0 1 0 0 0 16.9 0 0;
0 33.799 0 33.799 0 33.799 0 33.799 0 0 0 0 -33.799 0];
idx = matrix > 100;
matrix ( ~ ( sum ( idx , 2) == 0 ) , : ) = []
matrix =
Columns 1 through 7
0 0 0 16.9000 0 16.9000 0
0 33.7990 0 33.7990 0 33.7990 0
Columns 8 through 14
0 0 0 -16.9000 0 0 0
33.7990 0 0 0 0 -33.7990 0
>>
Yes, now it's also working for me without any errors.
Thank you so so much. God bless you! :)
Anytime :), make sure to give a vote because i struggled a lot to make this work
Hi Mr. ravi,
say, A= [ 110 0 10 70 15
100 30 50 20 9
50 -150 95 65 7]
I want to delete the rows having values greater than 100 and also smaller than -100. That means in the given example, 1st row has a value greater than 100 and 3rd row has a value smaller than -100. so I want to delet both 1st and 3rd rows. And finally,
A= [100 30 50 20 9]
How to do this? Please help me!
>> A = [ 110 0 10 70 15
100 30 50 20 9
50 -150 95 65 7];
idx = A > 100;
A ( ~ ( sum ( idx , 2) == 0 ) , : ) = [];
idx = A < 100;
A ( sum ( idx , 2 ) == size ( A , 2 ) , : ) = []
A =
100 30 50 20 9
>>
In my matrix, it's till now existing.It's showing:
[ -6.7e3 100 -100 -100 0
-2.1 100 -100 -100 0
0 100 0 0 1]
1st row should be deleted as it has very higher negative value. I want to delete this type of rows having higher negative values than 100.
Please help me!
But it's related to the original question to delete the number of rows! Do i need to ask again the same type of question that 'how to delete the number of rows'?
ok let me try for few minutes if not you can unaccept the answer
no prblem at all! you have already given me some right answers.
Try:
matrix=[ -6.7e3 100 -100 -100 0
-2.1 100 -100 -100 0
0 100 0 0 1];
idx = matrix < 100;
matrix ( sum ( idx , 2 ) ~= 0 , : ) = []
This code, although convoluted, will remove rows with any value greater than 100:
idx = matrix < 100;
matrix ( ~ ( sum ( idx , 2) == 0 ) , : ) = []
But what is this going to do?:
idx = matrix < 100;
matrix ( sum ( idx , 2 ) == size ( matrix , 2 ) , : ) = []
@Skyscraper: yes because if any of the numbers in a matrix is less than 100 the whole row is removed, the matrix which you gave contains numbers less than 100 in each row.

Sign in to comment.

More Answers (1)

@Sky Scrapper: the standard MATLAB practice is to use any:
>> A = [110,0,10,70,15;100,30,50,20,9;50,-150,95,65,7]
A =
110 0 10 70 15
100 30 50 20 9
50 -150 95 65 7
>> B = A(~any(abs(A)>100,2),:) % create a new array B,
B =
100 30 50 20 9
>> A(any(abs(A)>100,2),:) = [] % or delete rows from A.
A =
100 30 50 20 9

Categories

Find more on Mathematics 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!