Need Help for the rest of this coding

64 ビュー (過去 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 Center および File ExchangeCreating 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