byte matrix[4][4]= {
{0, 1, 1, 0},
{1, 0, 0, 1},
{1, 0, 0, 1},
{0, 1, 1, 0}
};

byte anodes[4] = {22, 23, 24, 25};
byte cathodes[4] = {26, 27, 28};
byte i; //just a variable which will be used in FOR loops

void setPinTo5V(byte pin)//set a pin given to this function to a 5V voltage source
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
}
void setPinToGND(byte pin)//set a pin given to this function to a GROUND (GND pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
}
void clearAllPins()//make all pins 'floating' - so they do nothing
{
  for(i=0;i<4;i++)//code inside is executed 4 times with value 'i' increased every time
  {
    pinMode(anodes[i], INPUT);
    digitalWrite(anodes[i], LOW);
    pinMode(cathodes[i], INPUT);
    digitalWrite(cathodes[i], LOW);
  }
}

void setup()
{
}

void loop()
{
  for(i=0;i<4;i++)//count i from 0 to 3
  {
    //1)
    clearAllPins();//make all pins do nothing (turn off every LED)
  
    //2), 3)
    if(matrix[0][i]==1)//check if 'i' column - 1st row LED is ON
      setPinTo5V(anodes[0]);//supply 5V to the 1st row
    if(matrix[1][i]==1)//check if 'i' column - 2nd row LED is ON
      setPinTo5V(anodes[1]);//supply 5V to the 2nd row
    if(matrix[2][i]==1)
      setPinTo5V(anodes[2]);
    if(matrix[3][i]==1)
      setPinTo5V(anodes[3]);

    setPinToGND(cathodes[i]);//set 'i' column pin to GND, so the needed LEDs are fully connected and turned on
    //4)
    delay(100);//keep the LEDs on for 1 ms
  }
}
