L_ONES and L_ZEROS
Create a vector or matrix of 1s or 0s of a specified size
=L_ONES(array) :: Returns a matrix of 1s the size of array =L_ONES(m,[n]) :: Returns a matrix of 1s the size of m x n (default n=1) =L_ZEROS(array) :: Returns a matrix of 0s the size of array =L_ZEROS(m,[n]) :: Returns a matrix of 0s the size of m x n (default n=1)
Argument | Description | Example |
---|---|---|
array | A 2D array of any size | A1:C4 |
m_rows | Number of rows | 4 |
n_columns | Number of columns | 3 |
In the template file, navigate to the Matrices worksheet to see the L_ONES and L_ZEROS function in action.
Description
Although matrices of 1s and 0s are very easy to create with the SEQUENCE function, the L_ONES and L_ZEROS functions are included in the LAMBDA library because of how commonly they are used when working with matrices.
L_ONES(array) =SEQUENCE( ROWS(array), COLUMNS(array), 1, 0 ) L_ZEROS(array) =SEQUENCE( ROWS(array), COLUMNS(array), 0, 0 ) L_ONES(n,m) =SEQUENCE( n, m, 1, 0 ) L_ZEROS(n,m) =SEQUENCE( n, m, 0, 0 )
Lambda Function Code
This code for using L_ONES and L_ZEROS in Excel is provided under the License as part of the LAMBDA Library, but to use just this function, you may copy the following code directly into your spreadsheet.
Code to Create Function via the Name Manager
Name: L_ONES Comment: Create a matrix of 1s that is the same size as the array or size m x n. Refers To: =LAMBDA(m_rows,[n_columns], LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(m_rows),cols,COLUMNS(m_rows), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,1,0), IF(ISOMITTED(n_columns), SEQUENCE(m_rows,1,1,0), SEQUENCE(m_rows,n_columns,1,0) ) ) )) Name: L_ZEROS Comment: Create a matrix of 0s that is the same size as the array or size m x n. Refers To: =LAMBDA(m_rows,[n_columns], LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(m_rows),cols,COLUMNS(m_rows), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,0,0), IF(ISOMITTED(n_columns), SEQUENCE(m_rows,1,0,0), SEQUENCE(m_rows,n_columns,0,0) ) ) ))
Code for AFE Workbook Module (Excel Labs Add-in)
/** * L_ONES(array) :: Returns a matrix of 1s the size of array * L_ONES(m,[n]) :: Returns a matrix of 1s the size of m x n (default n=1) */ L_ONES = LAMBDA(m_rows,[n_columns], LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(m_rows),cols,COLUMNS(m_rows), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,1,0), IF(ISOMITTED(n_columns), SEQUENCE(m_rows,1,1,0), SEQUENCE(m_rows,n_columns,1,0) ) ) )); /** * L_ZEROS(array) :: Returns a matrix of 0s the size of array * L_ZEROS(m,[n]) :: Returns a matrix of 0s the size of m x n (default n=1) */ L_ZEROS = LAMBDA(m_rows,[n_columns], LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(m_rows),cols,COLUMNS(m_rows), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,0,0), IF(ISOMITTED(n_columns), SEQUENCE(m_rows,1,0,0), SEQUENCE(m_rows,n_columns,0,0) ) ) ));
Named Function for Google Sheets
Name: L_ONES Description: L_ONES(array,) or L_ONES(n,m) - Create a matrix of 1s that is the same size as the array or size m x n. Arguments: param1, param2 Function: LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(param1),cols,COLUMNS(param1), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,1,0), IF(ISBLANK(param2), SEQUENCE(param1,1,1,0), SEQUENCE(param1,param2,1,0) ) ) ) Name: L_ZEROS Description: L_ZEROS(array,) or L_ZEROS(n,m) - Create a matrix of 0s that is the same size as the array or size m x n. Arguments: param1, param2 Function: LET(doc,"https://www.vertex42.com/lambda/ones-and-zeros.html", rows,ROWS(param1),cols,COLUMNS(param1), IF(OR(rows>1,cols>1), SEQUENCE(rows,cols,0,0), IF(ISBLANK(param2), SEQUENCE(param1,1,0,0), SEQUENCE(param1,param2,0,0) ) ) )
Warning
These L_ONES and L_ZEROS functions are not compatible between Excel and Google Sheets.
L_ONES and L_ZEROS Examples
Example 1
Generate a matrix of zeros that is the same size as the range A1:C4 (4 rows x 3 columns).
Test: Copy and Paste this LET function into a cell =LET( array, A1:C4, L_ZEROS(array) ) Result: {0, 0, 0; 0, 0, 0; 0, 0, 0; 0, 0, 0} =SEQUENCE(4,3,0,0)
Example 2
Generate a matrix of 1s that is n rows x m columns.
Test: Copy and Paste this LET function into a cell =LET( m_rows, 2, n_columns, 4, L_ONES(m_rows,n_columns) ) Result: {1, 1, 1, 1; 1, 1, 1, 1} =SEQUENCE(2,4,1,0)
References & Resources
Disclaimer: This article is meant for educational purposes only. See the License regarding the LAMBDA code, and the site Terms of Use for the documentation.