How can I check whether the strictly positive elements of each row of a matrix are equal?
Show older comments
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 Comments
José-Luis
on 29 Apr 2014
There are no negative values in your example.
MRC
on 29 Apr 2014
Geoff Hayes
on 29 Apr 2014
Cris - is this a homework question? Please review http://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers.
Accepted Answer
More Answers (2)
Sara
on 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
on 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... :)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!