How do I find the first value which is <1 in every column of a matrix?

4 commentaires

Cedric
Cedric le 20 Août 2015
Modifié(e) : Cedric le 20 Août 2015
Do you need row indices or values? Do all columns have such an element or are there columns with all elements >= 1?
Cedric
Cedric le 3 Sep 2015
Assuming that all columns have at least one element <1, here is one way:
>> X = rand( 6, 4 ) + 0.5 ; X(end,:) = 0.1 ; X % Build test case.
X =
1.1557 1.2431 0.7769 1.4502
0.5357 0.8922 0.5462 0.5344
1.3491 1.1555 0.5971 0.9387
1.4340 0.6712 1.3235 0.8816
1.1787 1.2060 1.1948 1.2655
0.1000 0.1000 0.1000 0.1000
>> firstLt1 = arrayfun( @(c)X(find(X(:,c)<1, 1), c), 1:size(X,2) )
firstLt1 =
0.5357 0.8922 0.7769 0.5344
If some column(s) may not have any element < 1, you can do the following instead:
>> firstLt1 = arrayfun( @(c)X(find(X(:,c)<1, 1), c), 1:size(X,2), 'UniformOutput', false ) ;
which outputs a cell array. Columns of X with no element < 1 lead to cells that contain empty arrays.
Image Analyst
Image Analyst le 3 Sep 2015
Cedric
Cedric le 3 Sep 2015
Modifié(e) : Cedric le 3 Sep 2015
Math puns are the first sine of madness!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Tags

Modifié(e) :

le 3 Sep 2015

Community Treasure Hunt

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

Start Hunting!

Translated by