Need Help for the rest of this coding

20 次查看(过去 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)

采纳的回答

David Hill
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)'];

更多回答(2 个)

James Tursa
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.

RAJA SEKHAR BATTU
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)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by