if we enter n rows and m colums and gets the output of 2n rows and 2m columns
9 views (last 30 days)
Show older comments
Write a function named blocks that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. The function needs to return a 2n-by-2m matrix where the upper right and lower left n-by m sub matrices are all zeros and the rest of the matrix are all ones.
0 Comments
Accepted Answer
Andrei Bobrov
on 27 Jun 2013
Edited: Andrei Bobrov
on 27 Jun 2013
2 Comments
Andrei Bobrov
on 4 Jul 2013
n = 5;
m = 3;
out = zeros([n m]*2);
for jj = 1:2
out((jj-1)*n+(1:n),(jj-1)*m+(1:m)) = 1;
end
More Answers (4)
Patricio Mosquera
on 26 Aug 2016
Moved: DGM
on 1 Jan 2024
Just choose your n and m
A = [1 0 ; 0 1];
B = ones([n, m]);
out = kron(A,B);
ANAMIKA YADAV
on 15 Oct 2019
function out=blocks(n,m);
out=zeros([n m]*2);
out(3:4,1:3)=1;
out(1:2,4:6)=1;
1 Comment
Himanshu Gabhane
on 4 Jun 2020
function out=blocks(n,m)
out=zeros([n m]*2);
out(end/2+1:end,1:end/2)=1;
out(1:end/2,end/2+1:end)=1;
end
1 Comment
DGM
on 1 Jan 2024
Edited: DGM
on 1 Jan 2024
While this at least calculates the output based on the input parameters (unlike @ANAMIKA YADAV's answer), the result is still completely inverted.
SWAPAN KUMAR
on 9 Mar 2023
Edited: DGM
on 1 Jan 2024
function result = blocks(n,m)
% create a matrix of ones of size n x m
ones_matrix =ones([n,m]);
% create a matrix of zeros of size n x m
zeros_matrix = zeros([n,m]);
% create a matrix of ones of size n x m and concatenate it with a matrix of zeros of size n x m
top_row = [ones_matrix zeros_matrix];
% create a matrix of zeros of size n x m and concatenate it with a matrix of ones of size n x m
bottom_row = [zeros_matrix ones_matrix];
% concatenate the top row and bottom row vertically to create a 2n x m matrix
matrix_first_half = [top_row; bottom_row];
% concatenate the bottom row and top row vertically to create a 2n x m matrix
matrix_second_half = [bottom_row; top_row];
% concatenate the first half and second half horizontally to create a 2n x 2m matrix
result = [matrix_first_half matrix_second_half];
end
1 Comment
DGM
on 1 Jan 2024
This creates the correct result, then it duplicates it and concatenates again, so the output is twice as wide as it should be. Why all the extra baloney that wasn't even part of the assignment? Just delete the unnecessary stuff and you're back to a correct result.
function result = blocks(n,m) % n,m are backwards by requirement
% create a matrix of ones of size n x m
ones_matrix =ones([n,m]);
% create a matrix of zeros of size n x m
zeros_matrix = zeros([n,m]);
% create a matrix of ones of size n x m and concatenate it with a matrix of zeros of size n x m
top_row = [ones_matrix zeros_matrix];
% create a matrix of zeros of size n x m and concatenate it with a matrix of ones of size n x m
bottom_row = [zeros_matrix ones_matrix];
% concatenate the top row and bottom row vertically to create a 2n x m matrix
result = [top_row; bottom_row];
end
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!