How can I check whether the strictly positive elements of each row of a matrix are equal?

조회 수: 4 (최근 30일)
How can I check whether the strictly positive elements of each row of a matrix are equal? E.g. if
A=[0 1 1 2; 0 1 0 1; 3 0 3 0]
I want
B=[0;1;1]
  댓글 수: 3
MRC
MRC 2014년 4월 29일
Sorry, I meant strictly positive, I have edited the question

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

채택된 답변

per isakson
per isakson 2014년 4월 29일
편집: per isakson 2014년 4월 29일
arrayfun( @(jj) (length(unique(A(jj,(A(jj,:)>0)))) == 1),(1:size(A,1)) )
returns
ans =
0 1 1

추가 답변 (2개)

Sara
Sara 2014년 4월 29일
B = zeros(size(A,1),1);
for i = 1:size(A,1)
temp = A(i,:);
temp = temp(temp>0);
temp = unique(temp);
if(length(temp) == 1)
B(i) = 1;
end
end

dpb
dpb 2014년 4월 29일
The "deadahead" solution...
isPos=false(size(A,1),1);
for i=1:size(A,1)
isPos(i)=all(diff(A(i,A(i,:)>0))==0);
end
Vectorize at your leisure... :)
  댓글 수: 2
dpb
dpb 2014년 4월 29일
Yes, w/ arrayfun. That was left as "exercise for the student" :)
As illustrated by Per; same idea with anonymous function. As also showed, any of a number of ways to check on the consistency of the values in a row is doable.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by