Asked by David Fowler
on 9 May 2019

I am using Matlab as a new hobby and not for school. I am very new and am reading through Computer Programming with MATLAB: J. Michael Fitzpatrick and Akos Ledeczi. There is a practice problem that I am stuck on:

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

Being very new to MATLAB. I have very little idea of where to start. I was thinking concatination, concatinating the same matrix 4 times but multiplying it by 1,2,3, then 4 as requested.

My current function is a very rough idea of what I want it to do. The main difference between my function and the correct function is the input is scalar rather than an array:

function A = custom_blocks(n,m);

A = [ones(n,m),2*ones(n,m);3*ones(n,m),4*ones(n,m)]; %concatinates 4 separate matrices

end

I made this function to get a rough idea of what is happening and to hopefully get the ball rolling. Though I am very stuck.

Answer by Stephen Cobeldick
on 9 May 2019

Edited by Stephen Cobeldick
on 9 May 2019

" I was thinking concatination, concatinating the same matrix 4 times but multiplying it by 1,2,3, then 4 as requested."

It would be simpler to perform those operations the other way around:

>> A = [1:3;3:-1:1]

A =

1 2 3

3 2 1

>> [A,2*A;3*A,4*A]

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

David Fowler
on 9 May 2019

I am one step closer:

function A = custom_blocks(n,m);

b = [n;m];

A = [b,2*b;3*b,4*b];

end

Command Window:

>> custom_blocks(2,3)

ans =

2 4

3 6

6 8

9 12

When entering the example input, I get the error:

>> custom_blocks([1:3;3:-1:1])

Not enough input arguments.

Error in custom_blocks (line 2)

b = [n;m];

Stephen Cobeldick
on 9 May 2019

Your assignment states that the function should have one input argument: "Write a function named custom_blocks that takes an n-by-m matrix as an input argument..."

You wrote a function which requires two input arguments... and then you are trying to call the function with just one matrix as an input.

In any case, why are you messing around with b and m and n? You are not gettting closer, you are getting further away... (from my answer and from the very simple solution to the assignment).

David Fowler
on 13 May 2019

I apologize, I am brand new to MATLAB, and writing programs in general. I overthought this problem by quite a bit. Thank you for pointing out that my function asked for two inputs, I didn't even realize that. Now that I have the correctly written program, I feel a bit silly for even posting this in the first place...cheers

function A = custom_blocks(b)

A = [b,2*b;3*b,4*b];

end

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.