How can I check whether the strictly positive elements of each row of a matrix are equal?
2 次查看(过去 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 个评论
Geoff Hayes
2014-4-29
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.
采纳的回答
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
0 个评论
更多回答(2 个)
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
0 个评论
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
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 Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!