In R I would like to set to zero all entries of matrix a few entries above (and below) matrix diagonal. Example below for N x N matrix with N = 5 and we delete k=3 lines of entries parallel to matrix diagonal :
a1 b1 c1 d1 e1 --> a1 b1 00 00 00
b2 a2 b2 c2 d2 --> b2 a2 b2 00 00
c3 b3 a3 b3 c3 --> 00 b3 a3 b3 00
d4 c4 b4 a4 b4 --> 00 00 b4 a4 b4
e5 d5 c5 b5 a5 --> 00 00 00 b5 a5
(00 means the same as 0)
for k=2 we have
a1 b1 c1 d1 e1 --> a1 b1 c1 00 00
b2 a2 b2 c2 d2 --> b2 a2 b2 c2 00
c3 b3 a3 b3 c3 --> c3 b3 a3 b3 c3
d4 c4 b4 a4 b4 --> 00 c4 b4 a4 b4
e5 d5 c5 b5 a5 --> 00 00 c5 b5 a5
I've wrote simple function based on two consecutive for() loops, but this function is too slow if I deal with large number of small matrices, all matrices are N x N with N in range 400:450 and k is always in range of 350:370, all matrix entries are in range between -1 and 1 (I deal with correlation matrices), the amount of data is a few GB, so I need some vectorized version of function. Is it faster to set entries to zero or to copy choosen entries to new matrix ?