if we enter n rows and m colums and gets the output of 2n rows and 2m columns

9 views (last 30 days)
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.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 27 Jun 2013
Edited: Andrei Bobrov on 27 Jun 2013
Please read about MATLAB functions: ones and blkdiag.
  2 Comments
jawad
jawad on 27 Jun 2013
Dear Andrei Bobrov My name is Jawad Durrani and I am doing MS in electrical communication. I have completed my course work and doing research now a days. I am learning Matlab for my research. I am studying the book "Computer Programming with Matlab by Michael Fitzpatrick and Akos Ledeczi" This is not my homework. Its one of the exercise problem. I already done by using built in function zeros and ones But i want to do it without using built in functions I hope you understand. Regards

Sign in to comment.

More Answers (4)

Patricio Mosquera
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
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
DGM
DGM on 1 Jan 2024
Edited: DGM on 1 Jan 2024
Not only does this presume that the input is always (2,3), the output is wrong regardless of the input. What's the point in posting an answer if you aren't going to even test to see if it works?

Sign in to comment.


Himanshu Gabhane
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
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.

Sign in to comment.


SWAPAN KUMAR
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
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

Sign in to comment.

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!