Need Help for the rest of this coding
조회 수: 13 (최근 30일)
이전 댓글 표시
Code has already been provided to define a function named vectorFun that accepts two input variables A and B described as follows:
- The variable A is a 6-element row vector of random integers between 0 and
.
- The variable B is a
-element column vector of random integers between 0 and
.
Add code to the function that uses the values in A and B to generate the following three vectors and assign to the indicated output variable names.
- Generate a vector named ABrow that is a 16 element row vector consisting of the elements of A followed by the elements of B.
- Generate a second vector named BAcol that is a 16 element column vector consisting of the elements of ABrow in reverse order.
- Generate a third vector named FirstHalfA_LastHalfB that is an 8-element row vector consisting of the first 3 elements of A followed by the last 5 elements of B .
Note the variables A and B are defined as function inputs. Do not overwrite their values in your code.
Your code should not include the following MATLAB functions or keywords: if, for, while
My Code so far:
function [ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
%Enter the commands for your function below Be sure to assign the values to each of the output variables.
%Defined in the function command on line 1.
ABrow = [3 17 4 5 10 15 33 12 6 31 37 27 49 22 13 28];
BAcol = [28 13 22 49 27 37 31 6 12 33 15 10 5 4 17 3];
FirstHalfA_LastHalfB = [3 17 4 27 49 22 13 28];
%create two vectors of random integers for function inputs
A = [3 17 4 5 10 15];
B = [33 12 6 31 37 27 49 22 13 28]';
[ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
댓글 수: 0
채택된 답변
David Hill
2022년 10월 27일
function [ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
ABrow = [A,B'];
BAcol = flip(ABrow)';
FirstHalfA_LastHalfB = [A(1:3),B(6:10)'];
댓글 수: 3
추가 답변 (2개)
James Tursa
2022년 10월 27일
편집: James Tursa
2022년 10월 27일
This is very basic vector construction and indexing. I suggest you take the MATLAB Onramp tutorials found here:
Some tips:
If A is a vector, then
A.' is the tranpose of A
A(2:4) is a vector containing the elements A(2) through A(4)
You can concatenate two variables X and Y into one variable with the syntax [X,Y] or [X;Y]
Reverse indexing can be done with a -1 in the middle, e.g. 4:-1:2 would be the indexes 4,3,2. Or you can use one of the flip( ) functions.
댓글 수: 0
RAJA SEKHAR BATTU
2022년 10월 27일
편집: RAJA SEKHAR BATTU
2022년 10월 27일
function [ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
%Enter the commands for your function below Be sure to assign the values to each of the output variables.
%Defined in the function command on line 1.
%ABrow = [3 17 4 5 10 15 33 12 6 31 37 27 49 22 13 28];
%BAcol = [28 13 22 49 27 37 31 6 12 33 15 10 5 4 17 3];
%FirstHalfA_LastHalfB = [3 17 4 27 49 22 13 28];
ABrow = [A B'];
BAcol = ABrow(end:-1:1);
FirstHalfA_LastHalfB = [A(1:3) B(6:end)'];
%create two vectors of random integers for function inputs
%A = [3 17 4 5 10 15];
%B = [33 12 6 31 37 27 49 22 13 28]';
%[ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!