in one line of code write a matrix that is 5x5 and has 1s in the first 3x3. the rest should be zeros in matlab

You can create a 5x5 matrix with 1s in the first 3x3 and zeros elsewhere in one line of code using the following MATLAB code:

main.m
M = [ones(3), zeros(3,2); zeros(2,5)];
39 chars
2 lines

Explanation:

  • ones(3) creates a 3x3 matrix of ones.
  • zeros(3,2) creates a 3x2 matrix of zeros to fill the remaining columns in the first 3 rows.
  • zeros(2,5) creates a 2x5 matrix of zeros for the last 2 rows.
  • By concatenating these matrices vertically using ;, we get a 5x5 matrix with 1s in the first 3x3 and zeros elsewhere.

related categories

gistlibby LogSnag