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

3 Ansichten (letzte 30 Tage)
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 Kommentare
MRC
MRC am 29 Apr. 2014
Sorry, I meant strictly positive, I have edited the question

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

per isakson
per isakson am 29 Apr. 2014
Bearbeitet: per isakson am 29 Apr. 2014
arrayfun( @(jj) (length(unique(A(jj,(A(jj,:)>0)))) == 1),(1:size(A,1)) )
returns
ans =
0 1 1

Weitere Antworten (2)

Sara
Sara am 29 Apr. 2014
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 am 29 Apr. 2014
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 Kommentare
dpb
dpb am 29 Apr. 2014
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by