Removing maxium and minimum from the matrix?

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일
편집: Geoff Hayes 2014년 6월 19일
What have you tried so far?
Consider iterating over each column and using the min and max functions. See http://www.mathworks.com/help/matlab/ref/min.html and http://www.mathworks.com/help/matlab/ref/max.html for details on how to use these functions, paying particular attention on how you can get the index of the minimum value(s) and maximum value(s).
Note that your code should be able to handle the case where a column has all identical values, more than one of the same minimum value, and more than one one of the same maximum value.
What does "remove" mean to you? If you want it to remain a matrix it must be rectangular, so you can set them to some specified value. If you truly want them removed, so that every column may have a different number of rows (in the case of multiple mins or maxes in the same row), then you're looking at a cell array.
Note that max and min both work on columns or rows as desired. So there is no need to use a loop, as long as the max/min are unique.
the cyclist
the cyclist 2014년 6월 19일
Giving us an example input and output matrix would also be helpful. (It doesn't have to be the full 24x4. Just a smaller illustrative example will do.)
David
David 2014년 6월 19일
Thank you guys for the comment, I realized it was my fault for not being clear with the question I've updated with some examples.
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일

1 개 추천

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일

2 개 추천

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.
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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 6월 19일

댓글:

2014년 6월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by