Removing maxium and minimum from the matrix?
Show older comments
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 Comments
Geoff Hayes
on 19 Jun 2014
Edited: Geoff Hayes
on 19 Jun 2014
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.
Image Analyst
on 19 Jun 2014
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.
John D'Errico
on 19 Jun 2014
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
on 19 Jun 2014
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
on 19 Jun 2014
Geoff Hayes
on 19 Jun 2014
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?
Accepted Answer
More Answers (2)
Geoff Hayes
on 19 Jun 2014
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 Comments
David
on 19 Jun 2014
Image Analyst
on 20 Jun 2014
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
on 19 Jun 2014
Edited: the cyclist
on 19 Jun 2014
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;
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!