Using semicolon for input argument when working with a matrix function

조회 수: 18 (최근 30일)
David Fowler
David Fowler 2019년 5월 9일
답변: Michael Mensah 2020년 6월 12일
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.

답변 (2개)

Stephen23
Stephen23 2019년 5월 9일
편집: Stephen23 2019년 5월 9일
" 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
Stephen23
Stephen23 2019년 5월 9일
편집: Stephen23 2019년 5월 9일
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 2019년 5월 13일
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

댓글을 달려면 로그인하십시오.


Michael Mensah
Michael Mensah 2020년 6월 12일
function B = costum_blocks(n,m)
A = [n:m;m:-1:n];
B = [A, 2*A; 3*A, 4*A];
return;

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by