Using semicolon for input argument when working with a matrix function
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Stephen23
el 9 de Mayo de 2019
Editada: Stephen23
el 9 de Mayo de 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 comentarios
Stephen23
el 9 de Mayo de 2019
Editada: Stephen23
el 9 de Mayo de 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
el 12 de Jun. de 2020
function B = costum_blocks(n,m)
A = [n:m;m:-1:n];
B = [A, 2*A; 3*A, 4*A];
return;
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!