How do you split up a matrix into 2 matrices where the rows are made up of the rows from the initial matrix containing certain numbers?

1 view (last 30 days)
How do you split up a matrix into 2 matrices were the rows are made up of the rows from the initial matrix containing certain numbers?
For example if I have the matrix,
J = [1 2 3 77 5; 77 1 2 77 3; 1 2 3 4 5; 1 2 3 4 5; 1 77 3 4 5; 1 2 3 4 5]
and I want,
A = [1 2 3 77 5; 77 1 2 77 3; 1 77 3 4 5]
and,
B = [1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5]
So far I have the code shown below, but I am not 100% sure if this is correct?
if true
% rows=size(J,1);
cols=size(J,2);
for i=1:1:rows
for u=1:1:cols
if J(i,u)==77
B=[
end
Any help would be appreciated, thanks!
  4 Comments
Zabih Siddiqi
Zabih Siddiqi on 24 Sep 2018
I done that initially Joel, but I want the code where it would work for any matrix regardless of size. Thanks for the help anyway!

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 24 Sep 2018
Contain77InRow=any(J==77,2);
A=J(Contain77InRow,:)
B=J(~Contain77InRow,:)

More Answers (1)

Adam Danz
Adam Danz on 24 Sep 2018
If your goal is to extract rows from J that contain 77 and place them in A while placing all other rows in B,
rowIdx = any(J==77,2);
A = J(rowIdx, :);
B = J(~rowIdx, :);

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!