필터 지우기
필터 지우기

how can i find a special row

조회 수: 2 (최근 30일)
Light
Light 2013년 6월 6일
How can i find a row which including only -1. In that A matrix how can i write a code
A=[-1,-1,0,0,0;0,0,0,-1,0;0,1,-1,0,-1;0,0,1,0,0;1,0,0,1,1]

채택된 답변

Ravi Jhou
Ravi Jhou 2013년 6월 7일
You could try
blnA = logical( A == -1 );
blnOut = find( sum( blnA' ) == 1 );
variable 'blnA' finds the elements of matrix A, which are equal to -1.
The output of blnA is
blnA =
1 1 0 0 0
0 0 0 1 0
0 0 1 0 1
0 0 0 0 0
0 1 0 0 0
if there is just one '-1', the summary for each row of logical matrix blnA will be '1'.
And because the command 'sum' sums the elements of column.
So need to do a transpose.
The output of blnOut
blnOut =
2
A ha, row 2. Got it.
  댓글 수: 4
Ravi Jhou
Ravi Jhou 2013년 6월 7일
You confused me.
Let me describe what you want.
e.g.
A = [-1 1 1 ;
0 -1 0 ;
0 0 -1 ;
1 0 0];
you want to find row 1, 2 ,and 3, which are included at least one '-1'.
or, you want to find row 2 and 3, which are included only one non-zero element '-1'.
You have to describe your question clearly, so I could answer you exactly.
Light
Light 2013년 6월 7일
Thank you for your big help ^_^
i want to find row 2 and 3, which are included only one non-zero element '-1
And i found it THANK YOU

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2013년 6월 6일
I am guessing that you mean, how can you find those rows each of which includes at least one -1. Is that what you mean? If so, do this:
f = find(any(A==-1,2));
  댓글 수: 3
Light
Light 2013년 6월 7일
In a big matrix so many row which including only one -1 can be found. Then i have to choose one of them. How can i code it?
Walter Roberson
Walter Roberson 2013년 6월 7일
rows = find( sum(A == -1, 2) == 1 );

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by