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
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
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
see edited answer
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?
>> 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
>>
works for me without any errors!
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
Thank you!
You are welcome.
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
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
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!
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
2018년 11월 23일
편집: madhan ravi
2018년 11월 23일
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.
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 , : ) = []
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
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개)
>> 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
Yes, it's working properly. Thank you very much for your help!
카테고리
도움말 센터 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
