Home » Matrix Calculation! Part 1

Matrix Calculation! Part 1

Solving Matrix Calculation Part 1 challenge by Power Query, Power BI, Excel, Python and R

Create a formula that, for any given value of ‘n’, generates an ‘n x n’ identity matrix where all values are 0, except for the diagonal (primary) values, which should be 1.

📌 Challenge Details and Links
Challenge Number: 188
Challenge Difficulty: ⭐
📥Download Sample File
📥Link to the solutions on LinkedIn

Solving the challenge of Matrix Calculation! Part 1 with Power Query

Power Query solution 1 for Matrix Calculation! Part 1, proposed by Zoran Milokanović:
let
  Source = 8, 
  _ = Table.FromRows(
    List.TransformMany(
      {1 .. Source}, 
      each {List.Repeat({0}, Source)}, 
      (i, _) => List.ReplaceRange(_, i - 1, 1, {1})
    )
  )
in
  _
Power Query solution 2 for Matrix Calculation! Part 1, proposed by Ramiro Ayala Chávez:
let
n = 5,
a = List.Repeat({0},Number.Power(n,2)),
b = List.Zip({List.Positions(a),a}),
c = List.Alternate(b,n,1,1),
d = List.Difference(b,c),
e = List.Transform(c, each {_{0}}&{_{1}+1}),
f = List.Sort(d&e,{each _{0}}),
g = List.Transform(f, each _{1}),
Sol = Table.FromRows(List.Split(g,n))
in
Sol
Power Query solution 3 for Matrix Calculation! Part 1, proposed by Aditya Kumar Darak 🇮🇳:
let
  n      = 3, 
  Gnr8   = List.Transform({1 .. n}, each List.Transform({1 .. n}, (f) => Number.From(_ = f))), 
  Return = Table.FromList(Gnr8, each _)
in
  Return
Power Query solution 4 for Matrix Calculation! Part 1, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = 5,
Lista = {1..Source},
Sol = Table.FromColumns(List.Transform(Lista, (x)=> 
 let
 a = List.Transform(Lista, (y)=> {x,y}),
 b = List.Transform(a, each if _{0}=_{1} then 1 else 0)
 in b))
in
Sol
Power Query solution 5 for Matrix Calculation! Part 1, proposed by Kris Jaganah:
let
  A = 8
in
  Table.FromRows(
    List.Split(List.TransformMany({1 .. A}, each {1 .. A}, (x, y) => Number.From(x = y)), A)
  )
Power Query solution 6 for Matrix Calculation! Part 1, proposed by Meganathan Elumalai:
let
  N = 8, 
  Result = Table.FromRows(
    List.Split(List.TransformMany({1 .. N}, (x) => {1 .. N}, (x, y) => if x = y then 1 else 0), N)
  )
in
  Result
Power Query solution 7 for Matrix Calculation! Part 1, proposed by Seokho MOON:
let
  matrix = (n) =>
    if n = 1 then
      Table.FromList({1}, each {_})
    else
      [
        A = Table.ToList(@matrix(n - 1), each _ & {0}), 
        B = {{0} & List.RemoveLastN(List.Last(A))}, 
        C = Table.FromList(A & B, each _)
      ][C]
in
  matrix(8)
Power Query solution 8 for Matrix Calculation! Part 1, proposed by Alexandre Garcia:
let
G = 8,
C = Table.FromColumns(List.Generate(()=> Number.From("1" & Text.Repeat("0", G-1)), each _ >=1, each _/10, each Text.ToList(Text.PadStart(Text.From(_),G,"0"))))
in C
Power Query solution 9 for Matrix Calculation! Part 1, proposed by Vida Vaitkunaite:
let
  List = List.Transform({0 .. num - 1}, (x) => Table.FromRows({List.Repeat({0}, x) & {1}})), 
  Tbl = Table.FromList(List, Splitter.SplitByNothing()), 
  Final = Table.TransformColumns(
    Table.Combine(Tbl[Column1]), 
    {}, 
    each Replacer.ReplaceValue(_, null, 0)
  )
in
  Final

Solving the challenge of Matrix Calculation! Part 1 with Excel

Excel solution 1 for Matrix Calculation! Part 1, proposed by Julian Poeltl:
=LET(
    Q,
    B3,
    D,
    TEXTAFTER(
        Q,
        "="
    ),
    MAKEARRAY(
        D,
        D,
        LAMBDA(
            A,
            B,
            IF(
                A=B,
                1,
                0
            )
        )
    )
)
Excel solution 2 for Matrix Calculation! Part 1, proposed by Kris Jaganah:
=LET(
    a,
    TEXTAFTER(
        B3,
        "="
    ),
    MAKEARRAY(
        a,
        a,
        LAMBDA(
            x,
            y,
            IF(
                x-y,
                0,
                1
            )
        )
    )
)
Excel solution 3 for Matrix Calculation! Part 1, proposed by Imam Hambali:
=LET(
    n,
    8,
     WRAPROWS(
         --BYROW(
             SEQUENCE(
                 n^2
             )=SEQUENCE(
                 ,
                 n,
                 1,
                 n+1
             ),
             OR
         ),
         n
     )
)
Excel solution 4 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(     n,
     5,     IF(          SEQUENCE(
              n,
               n
          ) = SEQUENCE(
              n,
               ,
               ,
               n + 1
          ),          1,          0     ))
Excel solution 5 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(     n,
     8,     MAKEARRAY(          n,          n,          LAMBDA(
              r,
               c,
               IF(
                   r = c,
                    1,
                    0
               )
          )     ))
Excel solution 6 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(     n,
     5,     _a,
     SQRT(
         SEQUENCE(
             n
         ) * SEQUENCE(
             ,
              n
         )
     ),     IF(
         _a = INT(
             _a
         ),
          1,
          0
     ))
Excel solution 7 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(     n,
     8,     IF(
         SEQUENCE(
             n
         ) = SEQUENCE(
             ,
              n
         ),
          1,
          0
     ))
Excel solution 8 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(
 n, 8,
 IF(
 SEQUENCE(n) - SEQUENCE(, n) = 0,
 1,
 0
 )
)
Excel solution 9 for Matrix Calculation! Part 1, proposed by Sunny Baggu:
=LET(
    n,
     8,
     MUNIT(
         n
     )
)
Excel solution 10 for Matrix Calculation! Part 1, proposed by abdelaziz allam:
=MAKEARRAY(
    3,
    3,
    LAMBDA(
        a,
        b,
        IF(
            a=b,
            1,
            0
        )
    )
)
Excel solution 11 for Matrix Calculation! Part 1, proposed by Andy Heybruch:
=LET(
    n,
    8,
    MAKEARRAY(
        n,
        n,
        LAMBDA(
            r,
            c,
            IF(
                r=c,
                1,
                0
            )
        )
    )
)
Excel solution 12 for Matrix Calculation! Part 1, proposed by Asheesh Pahwa:
=LET(n,
    --TEXTAFTER(
        B3,
        "="
    ),--(SEQUENCE(
    n
)=SEQUENCE(
    ,
    n
)))
Excel solution 13 for Matrix Calculation! Part 1, proposed by Bilal Mahmoud kh.:
=LET(
    n,
    5,
    MAKEARRAY(
        n,
        n,
        LAMBDA(
            r,
            c,
            IF(
                r=c,
                1,
                0
            )
        )
    )
)
Excel solution 14 for Matrix Calculation! Part 1, proposed by Eddy Wijaya:
=LET(    n,
    5,    MAKEARRAY(
        n,
        n,
        LAMBDA(
            r,
            c,
            IF(
                r=c,
                1,
                0
            )
        )
    )
)
Excel solution 15 for Matrix Calculation! Part 1, proposed by Ernesto Vega Castillo:
=IF(
    SEQUENCE(
        B17
    )=SEQUENCE(
        ,
        B17
    ),
    1,
    0
)
Excel solution 16 for Matrix Calculation! Part 1, proposed by ferhat CK:
=LET(
    n,
    5,
    WRAPCOLS(
        DROP(
            REGEXEXTRACT(
                REPT(
                    10^n,
                    n
                ),
                ".",
                1
            ),
            ,
            -n
        ),
        n
    )
)
Excel solution 17 for Matrix Calculation! Part 1, proposed by Hamidi Hamid:
=LET(n,5,MAP((SEQUENCE(n,n)-SEQUENCE(n))/n,LAMBDA(a,a=INT(a)))*1)
Excel solution 18 for Matrix Calculation! Part 1, proposed by Hussein SATOUR:
=MAKEARRAY(n,n,LAMBDA(x,y,(x=y)*1))
Excel solution 19 for Matrix Calculation! Part 1, proposed by Leonid Koyfman:
=LET(
    n,
    5,
    N(
        MAKEARRAY(
            n,
            n,
            LAMBDA(
                r,
                c,
                r=c
            )
        )
    )
)
Excel solution 20 for Matrix Calculation! Part 1, proposed by Meganathan Elumalai:
=LET(
    n,
    8,
    s,
    SEQUENCE,
    MID(
        BASE(
            2^s(
                n,
                ,
                n-1,
                -1
            ),
            2,
            n
        ),
        s(
            ,
            n
        ),
        1
    )
)
Excel solution 21 for Matrix Calculation! Part 1, proposed by Nicolas Micot:
=LAMBDA(
    matrix_n;
    MAKEARRAY(
        matrix_n;
        matrix_n;
        LAMBDA(
            l_row;
            l_col;
            SI(
                l_row=l_col;
                1;
                0
            )
        )
    )
)

for n = 3:
=MAKEARRAY(
    3;
    3;
    LAMBDA(
            l_row;
            l_col;
            SI(
                l_row=l_col;
                1;
                0
            )
        )
)
Excel solution 22 for Matrix Calculation! Part 1, proposed by Pieter de B.:
=MUNIT(
    A1
)
Where A1 holds the n value

Or a bit more creative: 
=LET(
    n,
    5,
    s,
    SEQUENCE(
        n
    ),
    N(
        s=TOROW(
            s
        )
    )
)
Excel solution 23 for Matrix Calculation! Part 1, proposed by Rick Rothstein:
=MAKEARRAY(B9,
    B9,
    LAMBDA(r,
    c,
    0+(r=c)))
Excel solution 24 for Matrix Calculation! Part 1, proposed by Rick Rothstein:
=MUNIT(
    B9
)
Excel solution 25 for Matrix Calculation! Part 1, proposed by Rick Rothstein:

=0+(SEQUENCE(
    B9
)=SEQUENCE(
    ,
    B9
))
Excel solution 26 for Matrix Calculation! Part 1, proposed by Sam Ngo:
=MUNIT(
    A1
)
Excel solution 27 for Matrix Calculation! Part 1, proposed by Thang Van:
=LET(
    n,
    H2,
    MAKEARRAY(
        n,
        n,
        LAMBDA(
            a,
            b,
            IF(
                a=b,
                1,
                0
            )
        )
    )
)
Excel solution 28 for Matrix Calculation! Part 1, proposed by Tomasz Jakóbczyk:
=LET(
    p,
    --TEXTAFTER(
        B3,
        "="
    ),
    MAKEARRAY(
        p,
        p,
        LAMBDA(
            x,
            y,
            IF(
                x=y,
                1,
                0
            )
        )
    )
)

Solving the challenge of Matrix Calculation! Part 1 with Python

Python solution 1 for Matrix Calculation! Part 1, proposed by Konrad Gryczan, PhD:
import numpy as np
def create_matrix(n):
 return np.eye(n)
print(create_matrix(3))
Python solution 2 for Matrix Calculation! Part 1, proposed by Luan Rodrigues:
import numpy as np

n = 3
a = np.identity(n)
print(a)

Solving the challenge of Matrix Calculation! Part 1 with Python in Excel

Python in Excel solution 1 for Matrix Calculation! Part 1, proposed by Alejandro Campos:
ne 
hashtag
#PythonExcel solution.
for n=5

[list(row) for row in np.identity(5, dtype=int)]

Solving the challenge of Matrix Calculation! Part 1 with Google Sheets

Google Sheets solution 1 for Matrix Calculation! Part 1, proposed by Peter Krkos:
PowerQuery solution:
https://docs.google.com/spreadsheets/d/1zR5IZLz8OT76vhaPEHfsPrw8-RDKnLyyqS49IJjdhFk/edit?pli=1&gid=599399170#gid=599399170

Leave a Reply