creating a matrix with zero and one

how to create a matrix of zeros and ones in matlab
matlab alternating series
matrix with only ones and zeros
matlab alternating 1 and 0 array
create a matrix of zeros with 2 rows and 4 columns in matlab
matlab create vector of zeros and ones
zero matrix multiplication
concatenate matrix matlab

I have this data:

A <- c("G", "C", "K")
B <- c("S", "B", "A")
C <- c("A", "M", "C")

And I have this matrix.

      [A]   [B]  [C]
[1,]    T    S    R
[2,]    Y    N    C
[3,]    G    U    N
[4,]    C    Z    Q
[5,]    S    D    K
[6,]    M    A    M
[7,]    S    B    F
[8,]    K    J    A

My idea is to create a new heatmap 8 rows x 3 columns where in each columns if rows don't contains letters in variables, I give zero. On the contrary, if the letter correspond of which contained in variables, a 1 appers. I would to obtain something like this:

       [A]   [B]  [C]
[1,]    0    1    0
[2,]    0    0    1
[3,]    1    0    0
[4,]    1    0    0
[5,]    0    0    0
[6,]    0    1    1
[7,]    0    1    0
[8,]    1    0    1

Could you suggest me a way to do this?

An option is to use mapply to simultaneously iterate over the columns of the matrix and elements of list(A, B, C)

mapply(function(col, vec) +(col %in% vec),, list(A, B, C))
#     A B C
#[1,] 0 1 0
#[2,] 0 0 1
#[3,] 1 0 0
#[4,] 1 0 0
#[5,] 0 0 0
#[6,] 0 1 1
#[7,] 0 1 0
#[8,] 1 0 1

Explanation: col %in% vec checks for matching entries from vec in the column vector of the matrix. The unary operator + converts TRUE/FALSE to 1/0.

Or even shorter (thanks @thelatemail)

+mapply(`%in%`,, list(A, B, C))

Sample data
A <- c("G", "C", "K")
B <- c("S", "B", "A")
C <- c("A", "M", "C")
mat <- matrix(c(
    "T", "Y", "G", "C", "S", "M", "S", "K",
    "S", "N", "U", "Z", "D", "A", "B", "J",
    "R", "C", "N", "Q", "K", "M", "F", "A"),
    ncol = 3)
colnames(mat) <- c("A", "B", "C")

How can I create a matrix of alternating 1s and 0s for any size matrix , Learn more about matrix, matrix manipulation, matrices, ones, zeros, toeplitz, mod MATLAB. m(1:2:end) = 1 %extracts all odd elements and makes them a one. So I was trying to figure out a fast way to make matrices with randomly allocated 0 or 1 in each cell of the matrix. I reached out on Twitter, and got many responses (thanks tweeps!). Here is the solution I came up with. See if you can tell why it

We can use map2 from purrr

+(map2_dfr(asplit(mat, 2), list(A, B, C), `%in%`))
#   A B C
#1 0 1 0
#2 0 0 1
#3 1 0 0
#4 1 0 0
#5 0 0 0
#6 0 1 1
#7 0 1 0
#8 1 0 1

Creating a matrix with all combinations of zeros and ones, Given an integer n ≥ 1, produce a matrix with 2n rows and n columns that contains all combinations of zeros and ones of length n. For example� This is an algorithm that plods through the matrix cell-by-cell and fills in the appropriate zero or one. It is better suited for the DATA step than for SAS/IML. In fact, the DATA step implementation is almost identical, except would use an array of variables instead of columns of a matrix. Building the Matrix by Using All Combinations

I´d bet there is a simpler solution, but the following works.

mat1 is the letter matrix.

mat2 is the binary (1,0) matrix.

The function contains(x,y) takes x, the character vector you want to test and y, the column name as a string and subsequently checks the column.

A <- c("G", "C", "K")
B <- c("S", "B", "A")
C <- c("A", "M", "C")
mat1 <- matrix(c("T","S", "R", "Y", "N", "C", "G", "U", "N", "C", "Z", "Q", "S", "D", "K", "M", "A", "M", "S", "B", "F", "K", "J", "A"), nrow = 8, ncol = 3, dimnames = list(c(1:8), c("A", "B", "C")), byrow=TRUE)
mat2 <- matrix(0L, nrow = 8, ncol = 3, dimnames = list(c(1:8), c("A", "B", "C")))

row_count <- 1
contains <- function(x, y){
  for (i in mat1[,y]){
    if (i %in% x){
      mat2[row_count,y] <<- 1
    row_count <<- row_count +1

contains(A, "A")
contains(B, "B")
contains(C, "C")

Hope that helps, Cheers!

Zero matrix & matrix multiplication (video), That would make a zero matrix indeed. It is one and the same thing. Comment.Duration: 4:41 Posted: Jun 7, 2014 What is Zero Matrix? In the zero matrix, all the elements of the matrix are zero. In the case of MATLAB, zeros function is used to create all zero in rows and columns of the matrix. The general Syntax is… x = zeros(x) Return the zeros of matrix 'x' Where, x is matrix. Example. How to create a zero matrix in MATLAB? Where, matrix A is 3 by 2

For your matrix M, the following may help you:

sapply(colnames(M), function(x) as.numeric(![,x],eval(parse(text = x))))))

MATLAB Lesson 6 - Creating matrices, Create a 3 by 5 matrix with all elements equal to zero. The command zeros(m,n) creates an m by n array (matrix) of zeros. >> A1� How can I create a matrix of alternating 1s and Learn more about matrix, matrix manipulation, matrices, ones, zeros, toeplitz, mod MATLAB

Zero Matrix, For the implementation, let us first create data matrices for sys-GMM. We combine dy2 and y2, dy1 and y1, and dx1 and x1 in a suitable way� Because of the commutative property, there are many different answer combinations. One possible solution would be: ( 9 + 6 – 5 + 2 ) – ( 8 + 7 – 4 + 1 )

Special Utility Matrices, To create a constant matrix whose values are all the same use an expression Built-in Function: zeros ( n ); Built-in Function: zeros ( m , n ); Built-in Function:� A matrix makes it easier to display data meaningfully across multiple dimensions -- it supports a stepped layout. The matrix automatically aggregates the data and enables drill down. You can create matrix visuals in Power BI Desktop reports and cross-highlight elements within the matrix with other visuals on that report page. For example, you

The Empty Matrix :: Data Structures (Programming), A matrix having at least one dimension equal to zero is called an empty matrix. You can create empty matrices (and arrays) of other sizes using the zeros� Or some of my stuff: You could convert your matrix to a logical matrix in a various ways and then add zeros, for example: + 0L # if you don't have `NA` values in your matrix Or just

  • You could collapse to mapply(`%in%`,, list(A, B, C)) + 0 or even mapply(is.element,, list(A, B, C)) + 0 if you want to avoid the backticks for %in%
  • @thelatemail Ah very neat; unary + works here too: +mapply(`%in%`,, list(A, B, C))