Extract elements of the array using any
조회 수: 2 (최근 30일)
이전 댓글 표시
A=[ 83 7 32 61;
91 13 17 48;
126 9 34 60;
172 11 36 60;
213 43 34 12 ;
254 13 25 60;
287 14 36 59;
366 1 3 70];
In this i want to find the difference between 2 rows of the 1st element.If the difference between any of the two row is less than or equal to 8 than the i want to remove the entire row whose 1st element is greater.
Example: In the above array A ,difference between the 91-83=8. Now here i want to remove entire row which contains 91.
I tried with the following code
Diff_Arr=diff(A); %to find the difference between the rows)
New_Arr_A = A(~any((Diff_Arr(:,1) <=8),2),:) % To get a new matrix extracting 91 %
Output:
New_Arr_A =[91 13 17 48;
126 9 34 60;
172 11 36 60;
213 43 34 12 ;
254 13 25 60;
287 14 36 59;
366 1 3 70];
But with this it is removing the entire row which contains 83.Instead i want to retain the row which contains 83 and remove entire row which contains 91. It would be great if you let me know how this is done.
Looking forward to hear from you at the earliest
Thanks
Pankaja
댓글 수: 0
답변 (3개)
Star Strider
2015년 10월 17일
This seems to be reasonably robust, providing the elements in the first column are always increasing between rows:
dA1 = diff(A(:,1)); % Calculate Differences In First Column
idx1 = find((dA1 <= 8)); % Find Indices Of Differences <= 8
[A1max,idx2] = max(dA1(idx1:idx1+1)); % Find Max Between Adjacent Elements
A(idx2+idx1-1,:) = []; % Result
댓글 수: 0
dpb
2015년 10월 17일
편집: dpb
2015년 10월 18일
Problem is you need to keep the length of the logical vector the same as the original length; diff shortens it by one without compensation. One possible way (of many)--
>> A([false;diff(A(:,1))<=8],:)=[]
A =
83 7 32 61
126 9 34 60
172 11 36 60
213 43 34 12
254 13 25 60
287 14 36 59
366 1 3 70
>>
If it's needed to keep A and make a new array w/o the given line the negate the test a and select instead of clearing the found row--
B=A(~[false;diff(A(:,1))<=8],:);
댓글 수: 0
Andrei Bobrov
2015년 10월 17일
편집: Andrei Bobrov
2015년 10월 17일
A = [83 32 7 61
91 13 17 48
95 9 34 60
172 11 36 60
213 43 34 12
254 13 25 60
260 14 36 59
268 1 3 70];
out = A;
l0 = diff(out(:,1));
l1 = abs(l0) <= 8;
i0 = sign(l0).*l1;
ii = find(i0);
out(ii + (i0(ii)<0),:) = [];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!