Extract rows in a Matrix based on column values of another Matrix.
Show older comments
I want to ask for help with constructing a matrix based on the values of another matrix.
I should to take the values of the matrix q based on the values in matrix p. I want to keep the same order or matrix p, but not successfully extracting the values in q. I should look for the value in the first column in q and then take all the values of that row.
I was using q(p), but this takes the values of p as the location in q, and when to try to extract them, it did not work as I do not have so many values in q. I could not find another similar example in the post, so ask you guys if one of you can help me.
Thanks for your help!

Accepted Answer
More Answers (1)
David Hill
on 18 Jun 2020
count=1;
for k=p
a=find(q(:,1)==k);%assume there is only one match
if ~isempty(a)
newMatrix(count,:)=q(a,:);
count=count+1;
end
end
1 Comment
Juan Pablo
on 19 Jun 2020
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!