2

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.

corey979
  • 23,947
  • 7
  • 58
  • 101
Rached
  • 61
  • 5

2 Answers2

7
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}}

corey979
  • 23,947
  • 7
  • 58
  • 101
3

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}}

kglr
  • 394,356
  • 18
  • 477
  • 896