hi if have a matrix like this:
A=[1,0,0,0;0,1,1,1;0,0,1,0;0,0,1,1]
In this matrix i want to check that apart from diagonal values(1,1 & 2,2 etc.), if there are some other non-zero elements in the row then display that row's index. In A, it should display 2 and 4.
Thanks in advance.

 채택된 답변

Image Analyst
Image Analyst 2017년 6월 8일

0 개 추천

Try eye(), any(), and find():
A=[1,0,0,0;0,1,1,1;0,0,1,0;0,0,1,1]
diagonalElements = logical(eye(length(A)))
% Make copy of A so we don't change
% the original A (which could be bad).
Acopy = A;
% Set diagonal elements = 0.
Acopy(diagonalElements) = 0
rowsWith1s = find(any(Acopy, 2))

댓글 수: 3

Tha saliem
Tha saliem 2017년 6월 8일
Thankyou very much it really helped. What if i want to get rowWith0s? (Like row 1 and 3 in above example)
You don't need a copy in that case because we don't need to change A, just to check whether the number of 0s is one less than the number of columns.
rowsWith0s = find(sum(A == 0, 2) == size(A, 2)-1)
Tha saliem
Tha saliem 2017년 6월 9일
Yes got it. Thanks alot for helping.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2017년 6월 8일

댓글:

2017년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by