How to find x?

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

4 Comments

Cedric
Cedric on 20 Aug 2015
Edited: Cedric on 20 Aug 2015
Do you need row indices or values? Do all columns have such an element or are there columns with all elements >= 1?
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 on 3 Sep 2015
Cedric
Cedric on 3 Sep 2015
Edited: Cedric on 3 Sep 2015
Math puns are the first sine of madness!

Sign in to comment.

Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Asked:

on 20 Aug 2015

Edited:

on 3 Sep 2015

Community Treasure Hunt

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

Start Hunting!