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

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 件のコメント

José-Luis
José-Luis 2014 年 4 月 29 日
There are no negative values in your example.
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 件のコメント

MRC
MRC 2014 年 4 月 29 日
Can I avoid the loop?
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

MRC
2014 年 4 月 29 日

コメント済み:

dpb
2014 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by