Need Help for the rest of this coding
11 views (last 30 days)
Show older comments
Batuhan Yildiz
on 27 Oct 2022
Commented: Thabang Mazibuko
on 23 Apr 2023
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 Comments
Accepted Answer
David Hill
on 27 Oct 2022
function [ABrow, BAcol, FirstHalfA_LastHalfB] = vectorFun(A, B)
ABrow = [A,B'];
BAcol = flip(ABrow)';
FirstHalfA_LastHalfB = [A(1:3),B(6:10)'];
3 Comments
More Answers (2)
James Tursa
on 27 Oct 2022
Edited: James Tursa
on 27 Oct 2022
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 Comments
RAJA SEKHAR BATTU
on 27 Oct 2022
Edited: RAJA SEKHAR BATTU
on 27 Oct 2022
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 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!