How to extract some rows from a matrix and put them in another matrix?

2 ビュー (過去 30 日間)
Cantor Set
Cantor Set 2020 年 2 月 17 日
回答済み: JESUS DAVID ARIZA ROYETH 2020 年 2 月 17 日
I have a matrix
Q=[ 1 2 3 4; 4 3 2 1; 6 7 8 9]; N=[1 2 3 4; 6 7 8 9];
is there a function which output: Q=[4 3 2 1]; ? I mean a general function
I tried to delete all the rows which are in N from Q:
for j=1:size(N,1)
Q(all(Q==repmat(N(j,1:end) ,size(Q,1),1),2),:)=[];
end
Q;
Is there a one-line function? does this code I wrote is correct for any arbitrary N and Q?

採用された回答

Stephen23
Stephen23 2020 年 2 月 17 日
>> idx = ismember(Q,N,'rows');
>> Q(idx,:) = []
Q =
4 3 2 1

その他の回答 (1 件)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2020 年 2 月 17 日
setdiff(Q,N,'rows')

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by