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)
Show older comments
Zabih Siddiqi
on 24 Sep 2018
Reopened: Walter Roberson
on 22 Dec 2018
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
Accepted Answer
Fangjun Jiang
on 24 Sep 2018
Contain77InRow=any(J==77,2);
A=J(Contain77InRow,:)
B=J(~Contain77InRow,:)
More Answers (1)
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, :);
See Also
Categories
Find more on Matrices and Arrays 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!