Using semicolon for input argument when working with a matrix function

12 Ansichten (letzte 30 Tage)
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.

Antworten (2)

Stephen23
Stephen23 am 9 Mai 2019
Bearbeitet: Stephen23 am 9 Mai 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 Kommentare
Stephen23
Stephen23 am 9 Mai 2019
Bearbeitet: Stephen23 am 9 Mai 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
David Fowler am 13 Mai 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

Melden Sie sich an, um zu kommentieren.


Michael Mensah
Michael Mensah am 12 Jun. 2020
function B = costum_blocks(n,m)
A = [n:m;m:-1:n];
B = [A, 2*A; 3*A, 4*A];
return;

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by