Effacer les filtres
Effacer les filtres

Extract the first negative value in a matrix column

6 vues (au cours des 30 derniers jours)
Thorsten
Thorsten le 29 Mai 2013
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...

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 29 Mai 2013
Modifié(e) : Azzi Abdelmalek le 29 Mai 2013
Edit
out=arrayfun(@(x) min([ 0 A(find(A(:,x)<0,1),x)]),1:size(A,2))
  2 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 29 Mai 2013
Look at edited answer
Thorsten
Thorsten le 29 Mai 2013
Thank you very much

Connectez-vous pour commenter.

Plus de réponses (2)

Iain
Iain le 29 Mai 2013
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 le 29 Mai 2013
t = A < 0;
[~,jj] = find(t);
out = accumarray(jj,A(t),[],@(x)x(1))';

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by