Logical vector based on matrix rows

11 ビュー (過去 30 日間)
ARS
ARS 2013 年 2 月 22 日
Let's say I have the matrix
A =
-2 1 2
1 2 3
3 1 2
4 3 4
1 1 2
and I want a logical vector in which the nth element is 1 if A(n,2:end) == [1 2] (for instance) and 0 otherwise. So in this case the result would be [1 0 1 0 1]. Of course this is just a random example, but what would be an easy, general solution to achieve this, for arbitrary matrix A (in this case as above) and row vector v (in this case [1 2])?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 22 日
編集済み: Azzi Abdelmalek 2013 年 2 月 22 日
out=ismember(A(:,2:end),[1 2],'rows')'

その他の回答 (1 件)

Mark Whirdy
Mark Whirdy 2013 年 2 月 22 日
編集済み: Mark Whirdy 2013 年 2 月 22 日
Please Press "accept" if this answers your question
%% YOUR EXAMPLE A = [... -2 1 2; 1 2 3; 3 1 2; 4 3 4; 1 1 2]; a = [1,2];
fn = @(A,a)(sum(A(:,end-size(a,2)+1:end)==repmat(a,size(A,1),1),2)==size(a,2)); % anonymous function
>> fn(A,a)
ans =
1
0
1
0
1
%% ANOTHER EXAMPLE
A = [...
-2 1 2 3 ;
1 2 3 3;
3 1 2 4;
4 3 4 4;
1 1 2 3];
a = [1,2,3];
>> fn(A,a)
ans =
1
0
0
0
1

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by