Create a matrix of 50 rows and 50 columns. The odd columns should contain values 2 and the even columns values 0

23 views (last 30 days)
Thao Nguyen
Thao Nguyen on 20 Oct 2021
Answered: Image Analyst on 20 Oct 2021
Create a matrix of 50 rows and 50 columns. The odd columns should contain values 2 and the even columns values 0

Answers (1)

Image Analyst
Image Analyst on 20 Oct 2021
There are several ways to do this. What did you try? You can do it in 2 lines of code. Here is the first one:
m = zeros(50,50); % Create a 50x50 matrix of all zeros.
Use simple indexing to assign the value 2 to all elements in the columns 1,3,5,7,...49.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!