Write a function named custom_blocks

Write a function named custom_blocks that takes an n-by-m
matrix as an input argument (the function does not have to check the format
of the input) and returns a 2n-by-2m matrix as an output argument. The upper
left n-by-m sub matrix of the output matrix is the same as the input matrix.
The elements of the upper right n-by-m sub matrix are twice as large as
the corresponding elements of the input matrix. Similarly, the lower left submatrix
is the input matrix multiplied by three and the lower right n-by-m submatrix
is four times the input matrix. For example, here is an example run:
>> custom_blocks([1:3;3:-1:1])
ans =
1 2 3 2 4 6
3 2 1 6 4 2
3 6 9 4 8 12
9 6 3 12 8 4

9 Kommentare

My code is incorrect. can someone give a hint for this question?
My code is:-
function A = cancel_middle1(n,m)
A((1:n),(m+1:m*2))*2;
A((n+1:n*2),(1:m))*3;
A((n+1:n*2),(m+1:m*2))*4;
end
Walter Roberson
Walter Roberson am 6 Dez. 2021
Where are you assigning anything to A? You index into it to pull values out of it, and you do minor arithmetic and then you discard the results of the calculation... but first you need to have an A array to index into
Siddharth Varshney
Siddharth Varshney am 6 Dez. 2021
No its still showing:-
Error in cancel_middle1 (line 3)
A((1:n),(m+1:m*2))*2;
Siddharth Varshney
Siddharth Varshney am 6 Dez. 2021
Please can you tell me the value i should assign to A?
You should be using code of the general form
A(rows, columns) = array of values
You currently have the A(rows, columns) part but not the assignment or the right hand side
Is this correct?
function A = cancel_middle1(n,m)
A(1:n,1:m)=1;
A(1:n,m+1:m*2)= 2;
A(n+1:n*2,1:m)= 3;
A(n+1:n*2,m+1:m*2)=4;
end
Does your function satisfy the requirements given in the problem statement? [No, it does not. What does the assignment say your function needs to accept as the input?]
When you try to reproduce the results from the example given in the problem, can you?
I recommend copying each sentence of the problem statement as comments and then writing the code to perform each of those steps. Fill in the blanks.
% List the input(s) and output(s) of your function
function fill_in_this_blank = cancel_middle(fill_in_this_blank)
% The upper left n-by-m sub matrix of the output matrix is the same as the input matrix.
fill_in_this_blank
% The elements of the upper right n-by-m sub matrix are twice as large as the
% corresponding elements of the input matrix.
fill_in_this_blank
% Similarly, the lower left submatrix is the input matrix multiplied by three
fill_in_this_blank
% and the lower right n-by-m submatrix is four times the input matrix.
fill_in_this_blank
end
Walter Roberson
Walter Roberson am 7 Dez. 2021
The problem does not pass in the values of m and n . The problem passes in a matrix with a particular number of rows and a particular number of columns. If you call the number of rows received n, and the number of columns received m, then the number of rows out must be 2*n and the number of columns out must be 2*m -- that is, the output must have twice as many rows as the input, and twice as many columns as the input.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Voss
Voss am 7 Dez. 2021

0 Stimmen

function out = custom_blocks(in)
out = [in 2*in; 3*in 4*in];
end

4 Kommentare

Walter Roberson
Walter Roberson am 7 Dez. 2021
Please note that this is a homework problem.
Voss
Voss am 7 Dez. 2021
Please note that I am aware of that and answered it anyway.
Walter Roberson
Walter Roberson am 8 Dez. 2021
The experience of the long-time volunteers is that it is most often better to guide students into how to read questions, and how to find resources, rather than to give them exact solutions to problems. In cases where specific code is useful, it is our experience that is often better to give code for a different question that uses the same principles, so that the student at least has to recognize the principles.
Voss
Voss am 8 Dez. 2021
I appreciate your comments. In the future, I will take the approach of solving a similar question that uses the same principles, as you suggest.

Melden Sie sich an, um zu kommentieren.

Vaibhav
Vaibhav am 3 Jun. 2024

0 Stimmen

function out = custom_blocks(in)
out = [in 2*in; 3*in 4*in];
end

Kategorien

Produkte

Version

R2021b

Tags

Gefragt:

am 6 Dez. 2021

Beantwortet:

am 3 Jun. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by