How can I delete the rows of a matrix in matlab under a given condition
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
madhan ravi
on 14 Nov 2018
Edited: madhan ravi
on 23 Nov 2018
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
Sky Scrapper
on 15 Nov 2018
Thanks for your reply. Could you please let me know what is 'idx'?
madhan ravi
on 15 Nov 2018
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
madhan ravi
on 15 Nov 2018
see edited answer
Sky Scrapper
on 15 Nov 2018
Edited: Sky Scrapper
on 15 Nov 2018
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?
madhan ravi
on 15 Nov 2018
>> 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
>>
madhan ravi
on 15 Nov 2018
works for me without any errors!
Sky Scrapper
on 15 Nov 2018
Yes, now it's also working for me without any errors.
Thank you so so much. God bless you! :)
madhan ravi
on 15 Nov 2018
Anytime :), make sure to give a vote because i struggled a lot to make this work
Sky Scrapper
on 15 Nov 2018
Done!
madhan ravi
on 15 Nov 2018
Thank you!
Sky Scrapper
on 15 Nov 2018
You are welcome.
Sky Scrapper
on 23 Nov 2018
Edited: Sky Scrapper
on 23 Nov 2018
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!
madhan ravi
on 23 Nov 2018
Edited: madhan ravi
on 23 Nov 2018
>> 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
>>
Sky Scrapper
on 23 Nov 2018
Edited: Sky Scrapper
on 23 Nov 2018
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!
Sky Scrapper
on 23 Nov 2018
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'?
madhan ravi
on 23 Nov 2018
Edited: madhan ravi
on 23 Nov 2018
ok let me try for few minutes if not you can unaccept the answer
Sky Scrapper
on 23 Nov 2018
no prblem at all! you have already given me some right answers.
madhan ravi
on 23 Nov 2018
Edited: madhan ravi
on 23 Nov 2018
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 , : ) = []
Sky Scrapper
on 23 Nov 2018
It's show empty matrix!
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 ) , : ) = []
madhan ravi
on 23 Nov 2018
Edited: madhan ravi
on 23 Nov 2018
@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.
More Answers (1)
>> 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
1 Comment
Sky Scrapper
on 23 Nov 2018
Yes, it's working properly. Thank you very much for your help!
Categories
Find more on Mathematics in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)