How to find x?
Show older comments
How do I find the first value which is <1 in every column of a matrix?
4 Comments
Cedric
on 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
on 3 Sep 2015

Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!