Algorithm that creates in a systematic form, matrix with zero-one elements I'm working on Rcpp and I wrote the following code
#include &lt;RcppArmadilloExtensions/sample.h&gt;
using namespace Rcpp;



// [[Rcpp::export]]

arma::mat SIZA(int K){
  
  arma::mat P;
  P.ones(K,K);
  P = trimatu(P);

  for(int idx=1; idx&lt;K; ++idx){
  arma::mat W;
  arma::mat Hlp;  

  W.ones(K-idx,K-idx);
  W = trimatu(W);
  W = join_horiz(W,Hlp.zeros(K-idx,idx));
   
  P = join_vert(P,W);
  }
  
  return P;  
}

Essentially it creates matrices of the following form
SIZA(2)
     [,1] [,2]
[1,]    1    1
[2,]    0    1
[3,]    1    0

SIZA(3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    0    1    1
[3,]    0    0    1
[4,]    1    1    0
[5,]    0    1    0
[6,]    1    0    0

SIZA(4)
      [,1] [,2] [,3] [,4]
 [1,]    1    1    1    1
 [2,]    0    1    1    1
 [3,]    0    0    1    1
 [4,]    0    0    0    1
 [5,]    1    1    1    0
 [6,]    0    1    1    0
 [7,]    0    0    1    0
 [8,]    1    1    0    0
 [9,]    0    1    0    0
[10,]    1    0    0    0

What I'm curious and interested in is finding a way to make this chunk of code quicker. But from my knowledge, I do not see an obvious way to achieve that. What I'm thinking is to avoid using the FOR LOOP but it seems to be necessary.
Any ideas would be great!
First of all I would try to avoid all the matrix operations like trimming, joining, etc.
And instead just find a function that tells what value is on each position
bool tell(int x, int y, int K);

This would of course require more mathematical skill then programming, but you might eventually end up doing just this:
arma::mat P;
int maxY = K * (K-1) / 2
for (int y = 0; y &lt; maxY; ++y) {
  for (int x = 0; x &lt; K; ++x) {
    P.set(x, y, tell(x, y, K) ? 1 : 0)
  }
}

If tell(x,y) has O(1) complexity, this will probably be faster.
Another option that I can think of is similar, but more iterative.
Fill the topmost KxK part of the matrix with the pattern.
Then another (K-1)x(K-1) part of the metrix, fill with the (K-1) pattern while filling the last column with zeroes.
then another part of size (K-2)x(K-2) fill with pattern of size K-2 while filling the last two columns with zeroes.
And so on and so on until trying to fill pattern 0x0 where you terminate.
But you'll end up on something similar to the previous
int Y = 0;
for (int step = K; step &gt; 0; --step) {
  for (int y = 0; y &lt; step; ++y, ++Y) {
    for (int x = 0; x &lt; step; ++x) {
      P.set(x, Y, tell(x, y) ? 1 : 0)
    }
    for (int r = step; r &lt; K; ++r) {
      P.set(r, Y, 0)
    }
  }
}

here the tell function actually becomes quite trivial and third parameter is not even needed
bool tell(int x, int y)
{
  return x &gt;= y;
}

Whether the tell function for the first implementation is possible in O(1) and how to do it I leave to you and the mathematician inside of you :)