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

댓글 수: 4

Cedric
Cedric 2015년 8월 20일
편집: Cedric 2015년 8월 20일
Do you need row indices or values? Do all columns have such an element or are there columns with all elements >= 1?
Cedric
Cedric 2015년 9월 3일
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 2015년 9월 3일
Cedric
Cedric 2015년 9월 3일
편집: Cedric 2015년 9월 3일
Math puns are the first sine of madness!

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2015년 8월 20일

편집:

2015년 9월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by