2.58 find first value in column larger than some value and cut matrix from
there
Given matrix
\[ \left ( {\begin {array}{ccc} 1 & 5 \\ 2 & 3 \\ 4 & 8 \\ 7 & 2 \end {array}} \right ) \]
search in column 2 of matrix for the first time a value exceeds 6 and return the matrix up
to that row. The result should be
\[ \left ( {\begin {array}{ccc} 1 & 5 \\ 2 & 3 \end {array}} \right ) \]
| Mathematica
a = {{1, 5}, {2, 3}, {4, 8}, {7, 2}};
Min@Position[a, {x_, y_} /; y > 6]
(* 3 *)
a[[1 ;; % - 1]]
|
{{1, 5}, {2, 3}}
|
| Matlab
A = [1,5;2,3;4,8;7,2];
A(1:find(A(:,2)>6)-1,:)
|
ans =
1 5
2 3
|
| Maple
A:=Matrix([[1,5],[2,3],[4,8],[7,2]]);
idx := ListTools:-SelectFirst(1, x->x>6 , A[..,2],output=indices);
if idx[1]>1 then
A[1..idx[1]-1,..];
else
A[1..idx[1],..];
fi;
|
\[ \left [\begin {array}{cc} 1 & 5 \\ 2 & 3 \end {array}\right ] \] |