필터 지우기
필터 지우기

Extract the first negative value in a matrix column

조회 수: 6 (최근 30일)
Thorsten
Thorsten 2013년 5월 29일
I have a 501x100000 Matrix, containing positive and some negative values. The values were calculated using random numbers. I need the value, not the index of the first negative entry in each column. If there is no negative value in the column it should be displayed by a 0.
simplified example:
A = [ 1, 2, 5, 6; -1, 4, 8,-4; -2, 3, 9, 1; -2,-1, 3,-1 ]
The answer I need (for that example) is:
-1,-1,0,-4
Thanks in advance for any help...

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 29일
편집: Azzi Abdelmalek 2013년 5월 29일
Edit
out=arrayfun(@(x) min([ 0 A(find(A(:,x)<0,1),x)]),1:size(A,2))
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 29일
Look at edited answer
Thorsten
Thorsten 2013년 5월 29일
Thank you very much

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

추가 답변 (2개)

Iain
Iain 2013년 5월 29일
for i = 1:cols
Answer = A(find(A(:,i)<0,1),i);
if isempty(Answer)
Out(i) = 0;
else
Out(i) = Answer;
end
end

Andrei Bobrov
Andrei Bobrov 2013년 5월 29일
t = A < 0;
[~,jj] = find(t);
out = accumarray(jj,A(t),[],@(x)x(1))';

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by