2
$\begingroup$

I have a matrix of size $n\times 3$, I would like to Floor only the two entries in each row. Any idea how that could be done? I tried the following:

Floor[matrix[[All, 1;;2]], 1]

but this trimmed the matrix and the output was an $n\times 2$ vector.

$\endgroup$
7
$\begingroup$
matrix = RandomReal[{0, 9}, {5, 3}]

{{6.77034, 0.121527, 5.26578}, {5.34336, 6.89392, 5.95391}, {2.3593, 1.71727, 4.31392}, {4.91466, 7.65461, 7.16075}, {6.91384, 5.82546, 6.50945}}

MapAt[Floor, matrix, {All, 1 ;; 2}]
 (* or *)
MapAt[Floor, #, {{1}, {2}}] & /@ matrix
 (* or *)
{Floor@#1, Floor@#2, ##3} & @@@ matrix
 (* or *)
{Floor@#1, Floor@#2, #3} & @@@ matrix
 (* or *)
{Floor@#[[1]], Floor@#[[2]], #[[3]]} & /@ matrix
 (* or *)
Replace[matrix, {a_, b_, c__} -> {Floor@a, Floor@b, c}, {1}]

All give

{{6, 0, 5.26578}, {5, 6, 5.95391}, {2, 1, 4.31392}, {4, 7, 7.16075}, {6, 5, 6.50945}}

$\endgroup$
2
$\begingroup$

Also:

SeedRandom[1]
matrix = RandomReal[{0, 9}, {4, 3}]

{{7.35651, 1.00278, 7.10573}, {1.69023, 2.17225, 0.591649}, {4.88022, 2.08039, 3.56405}, {6.30426, 1.90643, 6.73791}}

matrix[[;; , ;; 2]] = Floor[matrix[[;; , ;; 2]]];
matrix

{{7, 1, 7.10573}, {1, 2, 0.591649}, {4, 2, 3.56405}, {6, 1, 6.73791}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.