Need Help for the rest of this coding

조회 수: 26 (최근 30일)
Batuhan Yildiz
Batuhan Yildiz 2022년 10월 27일
댓글: Thabang Mazibuko 2023년 4월 23일
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)'];
  댓글 수: 3
Batuhan Yildiz
Batuhan Yildiz 2022년 10월 27일
Hey if possible can you give the step by step of the coding?
Thabang Mazibuko
Thabang Mazibuko 2023년 4월 23일
This is great stuff man. Appreciate it a lot

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

추가 답변 (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