フィルターのクリア

finding elements in a matrix`

2 ビュー (過去 30 日間)
Ananya Malik
Ananya Malik 2016 年 8 月 16 日
編集済み: Thorsten 2016 年 8 月 16 日
I have a a matrix A= [1 3 10; 1 8 10; 2 4 9] and an array x = [2 9]. i want to check if x is present in a single row of A. In this case it is true, so continue. Can anyone help? TIA.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 16 日
編集済み: Azzi Abdelmalek 2016 年 8 月 16 日
A= [1 3 10; 1 8 10; 2 4 9]
x=[ 2 9]
for k=1:size(A,1)
idx=all(ismember(x,A(k,:)));
if idx
break
end
end
idx
  1 件のコメント
Ananya Malik
Ananya Malik 2016 年 8 月 16 日
編集済み: Ananya Malik 2016 年 8 月 16 日
Thanks @Azzi Abdelmalek for the solution. However I have one more query. If I have X=[1 2; 3 9; 3 10; 2 4] and A. If I run the code
for i = 1: length(X(:,1))
x= X(i,:);
for k=1:size(A,1)
idx=all(ismember(x,A(k,:)));
if idx
break;
else
s=[s; x 0];
end
end
end
The output I get is s=[1 2 0;1 2 0; 1 2 0;3 9 0; 3 9 0;3 9 0;2 4 0;2 4 0]. The result i want is s= [1 2 0; 3 9 0].
Can you please help.

サインインしてコメントする。

その他の回答 (1 件)

Thorsten
Thorsten 2016 年 8 月 16 日
編集済み: Thorsten 2016 年 8 月 16 日
X=[1 2; 3 9; 3 10; 2 4]
x = [2 9];
X = X(any(ismember(X, x),2), :);
s = [X zeros(size(X,1), 1)];
You also keep the last row, because it contains a 2, a number present in x.

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by