Indexing with conditions for certain columns
Show older comments
I have a huge matrix where I want to find the indexes that meet each column median only and the rest of the column median should not be included. A manual way to do it is written as below but I need an easier way since my matrix is huge. Thank you so much in advanced.
Matrix_All = rand(1000,3) * 100;
Median_All = median(Matrix_All);
idx_1 = find( Matrix_All(:,1) == Median_All(1) & Matrix_All(:,2) ~= Median_All(2) & Matrix_All(:,3) ~= Median_All(3));
idx_2 = find( Matrix_All(:,1) ~= Median_All(1) & Matrix_All(:,2) == Median_All(2) & Matrix_All(:,3) ~= Median_All(3));
idx_3 = find( Matrix_All(:,1) ~= Median_All(1) & Matrix_All(:,2) ~= Median_All(2) & Matrix_All(:,3) == Median_All(3));
Mat_1 = Matrix_All(idx_1,:);
Mat_2 = Matrix_All(idx_2,:);
Mat_3 = Matrix_All(idx_3,:);
7 Comments
Walter Roberson
on 22 Mar 2019
You have an even number of rows. The median would be defined as the mean of the values adjacent to the center in a sorting. There might not be any matrix values that exactly equal that.
Walter Roberson
on 22 Mar 2019
What would you want to happen in the case where a row happens to match more than one of the column medians ?
Yaser Khojah
on 22 Mar 2019
Edited: Yaser Khojah
on 22 Mar 2019
Walter Roberson
on 22 Mar 2019
You show Matrix_All being initialized to 3 columns. Your newer version has a MaT_All indexed up to 8 columns. We as outsiders do not know that MaT_All has at least 8 columns.
Yaser Khojah
on 22 Mar 2019
Edited: Yaser Khojah
on 22 Mar 2019
Guillaume
on 22 Mar 2019
There are no limitation based on the number of columns to doing what you want... whatever that is...
You haven't answered Walter's questions, so we're in the dark about what exactly you're trying to do:
- what if the median is not found anywhere? e.g. median([1 2 3 4]) is 2.5.
- what if the median is found multiple time? e,.g median([1 1 2 2 3 3]) is 2
Perhaps, you should explain what the purpose of all this is. Maybe it's not the median that you actually need.
Note that find was completely unnecessary in your code so far.
idx = Matrix_All(:,1) == Median_All(1) & Matrix_All(:,2) ~= Median_All(2) & Matrix_All(:,3) ~= Median_All(3)
Mat_1 = Matrix_All(idx_1,:);
would have produced the same result (or error).
Yaser Khojah
on 25 Mar 2019
Edited: Yaser Khojah
on 25 Mar 2019
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!