Keeping maximum values on diagonals in matrix

Viewed 172

Let's say I have a matrix A which represents elevation values:

A = matrix(c(100,105,106,109,101,101,106,104,107,106,101,102,105,106,108,102,102,104,110,104),
           nrow=5, ncol=4)

I would like to create a new matrix created out diagonal analysis of the A matrix. Starting in ex. the upper-left corner I want to analyse each diagonal and keeping the maximum values step by step. The expected results should be as following:

B = matrix(c(100,105,106,109,101,101,106,105,107,109,101,102,106,106,108,102,102,102,110,106),
           nrow=5, ncol=4)

Does anyone could help me with that?

1 Answers
Related