Extract elements of the array using any

1 次查看(过去 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

回答(3 个)

Star Strider
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

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

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

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by