Using semicolon for input argument when working with a matrix function
20 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Stephen23
on 9 May 2019
Edited: Stephen23
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
4 Comments
Stephen23
on 9 May 2019
Edited: Stephen23
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).
Michael Mensah
on 12 Jun 2020
function B = costum_blocks(n,m)
A = [n:m;m:-1:n];
B = [A, 2*A; 3*A, 4*A];
return;
0 Comments
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!