# Extract elements of the array using any

12 ビュー (過去 30 日間)
Pankaja Tanjore 2015 年 10 月 17 日

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

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

### 回答 (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 件のコメント表示非表示 -1 件の古いコメント

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

dpb 2015 年 10 月 17 日

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 件のコメント表示非表示 -1 件の古いコメント

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

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),:) = [];

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

### Community Treasure Hunt

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

Start Hunting!

Translated by