2.23 Select entries in one column based on a condition in another column
Given
A=[4 3
6 4 ---->
7 6 ---->
2 1
1 3
9 2
2 5 ---->
1 2
]
Select elements in the first column only which has corresponding element in the second
column greater than 3, hence the result will be
[6 7 2]
| Mathematica
mat = {{4, 3},
{6, 4},
{7, 6},
{2, 1},
{1, 3},
{9, 2},
{2, 5},
{1, 2}};
r = Select[mat, #[[2]] > 3 &];
r[[All, 1]]
|
{6, 7, 2}
|
| another way is to find the index using Position and then use
Extract
loc = Position[mat, {x_, y_} /; y > 3]
r = Extract[mat, loc];
r[[All, 1]]
|
{6, 7, 2}
|
| another way is to use Cases[]. This is the shortest way
Cases[mat, {x_, y_} /; y > 3 :> x]
|
{6, 7, 2}
|
| Matlab
A=[4, 3;
6, 4;
7, 6;
2, 1;
1, 3;
9, 2;
2, 5;
1, 2];
A(A(:,2)>3,1)
|
6
7
2
|
Maple
In Maple, we must convert the Matrix to list of lists to work on each row at a time. I could
not find a way to avoid this.
restart;
A:=<<4|3>,
<6|4>,
<7|6>,
<2|1>,
<1|3>,
<9|2>,
<2|5>,
<1|2>>;
select(z->evalb(z[2]>3),convert(A,listlist))[..,1]
|
\([6, 7, 2]\) |