Removing maxium and minimum from the matrix?

7 次查看(过去 30 天)
Hi, I have a 24x4 matrix I would like to remove the minimum and maximum of 2nd column and it's corresponding value in the 1st column from the matrix. Any help would be appreciated.
Here's a example table D =
100.00 7.80
200.00 7.50
300.00 8.50
400.00 8.40
500.00 11.10
600.00 14.50
700.00 22.60
800.00 34.90
900.00 30.00
1000.00 29.50
1100.00 21.30
1200.00 20.50
1300.00 18.40
1400.00 15.60
1500.00 14.00
1600.00 13.90
1700.00 13.00
1800.00 11.80
1900.00 12.40
2000.00 13.40
2100.00 5.60
2200.00 6.70
2300.00 6.40
2400.00 5.90
When I do [a,b]=min(D)
a =
100.00 5.60
b =
1.00 21.00
[c,d]=max(D)
c =
2400.00 34.90
d =
24.00 8.00
when I do sortrows(D,2) it looks like this
sortrows(D,2)
ans =
2100.00 5.60
2400.00 5.90
2300.00 6.40
2200.00 6.70
200.00 7.50
100.00 7.80
400.00 8.40
300.00 8.50
500.00 11.10
1800.00 11.80
1900.00 12.40
1700.00 13.00
2000.00 13.40
1600.00 13.90
1500.00 14.00
600.00 14.50
1400.00 15.60
1300.00 18.40
1200.00 20.50
1100.00 21.30
700.00 22.60
1000.00 29.50
900.00 30.00
800.00 34.90
I want it to look like this
2400.00 5.90
2300.00 6.40
2200.00 6.70
200.00 7.50
100.00 7.80
400.00 8.40
300.00 8.50
500.00 11.10
1800.00 11.80
1900.00 12.40
1700.00 13.00
2000.00 13.40
1600.00 13.90
1500.00 14.00
600.00 14.50
1400.00 15.60
1300.00 18.40
1200.00 20.50
1100.00 21.30
700.00 22.60
1000.00 29.50
900.00 30.00
notice both the max and min values of column 2(which were 5.60 and 34.90, along with their corresponding values from column 1) gone from the matrix.
  7 个评论
Geoff Hayes
Geoff Hayes 2014-6-19
The minimum and the maximum value in the first column are 100 and 2400 respectively. Yet they still appear in the first column in your example. Why?
And are you assuming that all values in your columns are distinct? What happens if more than one row have the same minimum or maximum?
David
David 2014-6-19
编辑:David 2014-6-19
@Geoff, thanks for the comment. Yes 100 and 2400 both remained because they are from column 1, 5.60 and 34.90 got removed and their corresponding column 1 values were also removed(2100 and 800). Also All the values are distinct. I'm using the min() and max() function to determine what I need to remove from the matrix. I think the phrasing of my question was bad, I changed it a bit.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-6-19
How about this:
format long g
workspace;
D =[...
100.00 7.80
200.00 7.50
300.00 8.50
400.00 8.40
500.00 11.10
600.00 14.50
700.00 22.60
800.00 34.90
900.00 30.00
1000.00 29.50
1100.00 21.30
1200.00 20.50
1300.00 18.40
1400.00 15.60
1500.00 14.00
1600.00 13.90
1700.00 13.00
1800.00 11.80
1900.00 12.40
2000.00 13.40
2100.00 5.60
2200.00 6.70
2300.00 6.40
2400.00 5.90]
[minValue, rowsToDelete1] = min(D(:,2))
[maxValue, rowsToDelete2] = max(D(:,2))
D([rowsToDelete1, rowsToDelete2], :) = []; % Get rid of min(s) and max(es).
D % Print out

更多回答(2 个)

Geoff Hayes
Geoff Hayes 2014-6-19
Since it is the minimum and maximum values in the second column that determine which rows are removed, then you could do something like following where D is the matrix that you have defined above (just after the sortrows(D,2)
% get the row indices of the maximum value(s) from the second column of D
% note that we ignore the first output from max by using ~
[~,idcs] = max(D(:,2));
% remove that row with the maximum value(s)
D(idcs,:) = [];
% get the row indices of the minimum value(s) from the second column of D
% note that we ignore the first output from min by using ~
[~,idcs] = min(D(:,2));
% remove that row with the minimum value(s)
D(idcs,:) = [];
I know you said that all elements are distinct, but the above code will remove all rows that have the same minimum value or same maximum value.
Try the above and see what happens!
NOTE that your original matrix is 24x4, whereas this example is for a 24x2 matrix.
  2 个评论
David
David 2014-6-19
Thank you for the answer that worked as well.
Image Analyst
Image Analyst 2014-6-20
Thanks for clicking "Accept this answer". You can still vote for answers, even though you can only Accept one. That way he gets the reputation points. For that matter, you can also vote for mine or any others you accept.

请先登录,再进行评论。


the cyclist
the cyclist 2014-6-19
编辑:the cyclist 2014-6-19
Here is my best guess as to what you want, where I chose to replace the max and min with "NaN":
% Example data (Replace with your actual data.)
M = magic(7);
% Find indices to maxima
[~,idx] = max(M);
max_i_j = sub2ind(size(M),idx,1:size(M,2))
% Find indices to minima
[~,idx] = min(M);
min_i_j = sub2ind(size(M),idx,1:size(M,2));
M([max_i_j min_i_j]) = NaN;
  1 个评论
David
David 2014-6-19
Thank you for the response unfortunately that was not what I was looking for, it was my fault for not being very clear with the question.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by