Indexing with conditions for certain columns

Asked by Yaser Khojah

Yaser Khojah (view profile)

on 22 Mar 2019
Latest activity Commented on by Walter Roberson

Walter Roberson (view profile)

on 25 Mar 2019
Accepted Answer by Walter Roberson

Walter Roberson (view profile)

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

Yaser Khojah

Yaser Khojah (view profile)

on 22 Mar 2019
Yeah i did that earlier only to give an example but then in my comment I added something from my code. Do you think there is a way to do that with 8 columns or it is a limitation in MATLAB? Here is my data to give you an idea. Thank you
Guillaume

Guillaume (view profile)

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

Yaser Khojah (view profile)

on 25 Mar 2019
Dear Guiliaume, Sorry I could not get back to you since I did not have access to my MATLAB. Below is what i want to do but not manually since I have to find do that for different criteria.
idx_1 = MaT_All(:,1) == Median_All(1) & MaT_All(:,2) ~= Median_All(2)...
& MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6)...
& MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) ~= Median_All(8);
Mat_1 = MaT_All(idx_1,:);
scatter(MaT_All(idx_1,18),MaT_All(idx_1,17)); hold on;
idx_2 = MaT_All(:,1) ~= Median_All(1) & MaT_All(:,2) == Median_All(2)...
& MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6)...
& MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) ~= Median_All(8);
Mat_1 = MaT_All(idx_2,:);
scatter(MaT_All(idx_2,18),MaT_All(idx_2,17)); hold on;
Is it because if the median is found multiple time you only pick one answer where in my case I select all of them?

Tags

Answer by Walter Roberson

Walter Roberson (view profile)

on 22 Mar 2019

matches_median = bsxfun(@eq, MaT_All, Median_All);
matches_one = find(sum(matches_median,2) == 1);
matches_which = 1 + sum( cumprod(~matches_median(matches_one,:), 2), 2 );
Mat = cell(8,1);
for G = 1 : 8
Mat{G} = MaT_All(matches_one(matches_which==G),:);
end

Yaser Khojah

Yaser Khojah (view profile)

on 25 Mar 2019
Dear Walter thank you so much for coding this problem. It is not really easy to do it and thank you so much. I'm sorry for coming back late since I could not access MATLAB. Thanks again :).
I’m just wondering when your answer as below, I do not get the same answer my code. Any idea?
figures (yours)
scatter(Mat{1,1}(:,18),Mat{1,1}(:,17))
figures (mine)
idx_1 = MaT_All(:,1) == Median_All(1) & MaT_All(:,2) ~= Median_All(2)...
& MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6)...
& MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) ~= Median_All(8);
Mat_1 = MaT_All(idx_1,:);
scatter(MaT_All(idx_1,18),MaT_All(idx_1,17)); hold on;
Walter Roberson

Walter Roberson (view profile)

on 25 Mar 2019
The data you have provided us only has 10 columns, so looking at column 18 vs column 17 does not make any sense to us.