Asked by Yaser Khojah
on 22 Mar 2019 at 19:41

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

Answer by Walter Roberson
on 22 Mar 2019 at 23:05

Accepted Answer

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
on 25 Mar 2019 at 9:38

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
on 25 Mar 2019 at 17:18

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684497

## Walter Roberson (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684499

## Yaser Khojah (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684501

## Walter Roberson (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684505

## Yaser Khojah (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684510

## Guillaume (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_684517

## Yaser Khojah (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/451778-indexing-with-conditions-for-certain-columns#comment_685369

Sign in to comment.