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
5 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers862
Suggested Problems
-
Get the area codes from a list of phone numbers
1072 Solvers
-
Project Euler: Problem 10, Sum of Primes
2092 Solvers
-
Number of 1s in a binary string
10998 Solvers
-
Calculate the area of a triangle between three points
3421 Solvers
-
How long do each of the stages of the rocket take to burn?
456 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!
Good problem!
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?
@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.
good one
N=3
A=ones(3)
B=zeros(3)
C=N*ones(3)
M=([A B;B C])
I'm just starting out self learning and to my understanding this code answers the question since i made N an input but it says i did it wrong. i guess i have to use this 'foursquare' function (only seeing it for the first time now). can someone please explain what the function does?