Problem 55220. Matrix Quadrants
Write a function that takes N as the input, and outputs a matrix whose upper-left (NxN) quadrant contains all ones, the lower-right (NxN) quadrant contains all N's, and zeros everywhere else. For example, if N = 3:
Solution Stats
Problem Comments
-
4 Comments
Show
1 older comment
Andreea
on 16 Jan 2023
function M = foursquare(N)
M = zeros(2*N);
for i=1:length(M)/2
for j=1:length(M)/2
M(i,j)=1;
j=j+1;
end
i=i+1;
end
for i=(length(M)/2)+1:length(M)
for j=(length(M)/2)+1:length(M)
M(i,j)=3;
j=j+1;
end
i=i+1;
end
end
Just wrote that program that gives me the right solution but I don't understand why it won't work.Any suggestions?
Christian Schröder
on 17 Jan 2023
@Andreea you're hard-coding the value 3 on the twelfth line; the problem description says that the lower-left quadrant should contain N's instead.
Kshitij
on 20 Mar 2025 at 8:23
good one
Solution Comments
Show commentsProblem Recent Solvers674
Suggested Problems
-
8971 Solvers
-
2096 Solvers
-
Project Euler: Problem 7, Nth prime
1567 Solvers
-
410 Solvers
-
330 Solvers
More from this Author10
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!