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!

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 14 日
編集済み: madhan ravi 2018 年 11 月 23 日
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 件のコメント

Sky Scrapper
Sky Scrapper 2018 年 11 月 15 日
Thanks for your reply. Could you please let me know what is 'idx'?
madhan ravi
madhan ravi 2018 年 11 月 15 日
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
madhan ravi 2018 年 11 月 15 日
see edited answer
Sky Scrapper
Sky Scrapper 2018 年 11 月 15 日
編集済み: Sky Scrapper 2018 年 11 月 15 日
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
madhan ravi 2018 年 11 月 15 日
>> 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
madhan ravi 2018 年 11 月 15 日
works for me without any errors!
Sky Scrapper
Sky Scrapper 2018 年 11 月 15 日
Yes, now it's also working for me without any errors.
Thank you so so much. God bless you! :)
madhan ravi
madhan ravi 2018 年 11 月 15 日
Anytime :), make sure to give a vote because i struggled a lot to make this work
Sky Scrapper
Sky Scrapper 2018 年 11 月 15 日
Done!
madhan ravi
madhan ravi 2018 年 11 月 15 日
Thank you!
Sky Scrapper
Sky Scrapper 2018 年 11 月 15 日
You are welcome.
Sky Scrapper
Sky Scrapper 2018 年 11 月 23 日
編集済み: Sky Scrapper 2018 年 11 月 23 日
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
madhan ravi 2018 年 11 月 23 日
編集済み: madhan ravi 2018 年 11 月 23 日
>> 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
Sky Scrapper 2018 年 11 月 23 日
編集済み: Sky Scrapper 2018 年 11 月 23 日
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
Sky Scrapper 2018 年 11 月 23 日
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
madhan ravi 2018 年 11 月 23 日
編集済み: madhan ravi 2018 年 11 月 23 日
ok let me try for few minutes if not you can unaccept the answer
Sky Scrapper
Sky Scrapper 2018 年 11 月 23 日
no prblem at all! you have already given me some right answers.
madhan ravi
madhan ravi 2018 年 11 月 23 日
編集済み: madhan ravi 2018 年 11 月 23 日
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
Sky Scrapper 2018 年 11 月 23 日
It's show empty matrix!
Stephen23
Stephen23 2018 年 11 月 23 日
編集済み: Stephen23 2018 年 11 月 23 日
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
madhan ravi 2018 年 11 月 23 日
編集済み: madhan ravi 2018 年 11 月 23 日
@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.

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 11 月 23 日
編集済み: Stephen23 2018 年 11 月 23 日

0 投票

@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

1 件のコメント

Sky Scrapper
Sky Scrapper 2018 年 11 月 23 日
Yes, it's working properly. Thank you very much for your help!

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2018 年 11 月 14 日

編集済み:

2018 年 11 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by