Matlab matrices and vectors

조회 수: 4 (최근 30일)
Steph surry
Steph surry 2020년 10월 6일
댓글: Steven Lord 2020년 10월 6일
A =[1,2,2,3,1,2,2] B =[1,2,2,2,2,2,2]
How to Generate a vector composed of first 4 elements of A and last 5 elements of B.
  댓글 수: 4
Terin Richardson
Terin Richardson 2020년 10월 6일
There are several ways you could do this, the easiest just being C = horzcat(A(1:4), B(3:7)). horzcat stands for horizontal concatenation, meaning adding things horizontally. If these were column vectors, you would instead use the vertcat(A,B) function. You can also call these functions without using their names by creating arrays like this:
For horzcat: C = [A(1:4),B(3:7)]
For vertcat: C = [A(1:4);B(3,7)]
Note the difference in notation here is the horzcat uses a comma while the vertcat uses a semicolon.
If you want this to work for arrays of any size, meaning you always get the last 5 digits from B regardless of its size, you would need to use B(end-4:end) instead of B(3:7) in the line I listed above.
Steven Lord
Steven Lord 2020년 10월 6일
Since you're a beginner you may find the free MATLAB Onramp tutorial useful in familiarizing yourself with the basics of MATLAB.

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

답변 (1개)

KSSV
KSSV 2020년 10월 6일
Read about MATLAB indexing.......if A is given array.
A(1:3) % gives first three elements
A(end) % gives last element
A(1) % gives first element
size(A) % gives size
[A(1:2) A(4:end)] % joins two arrays
You should read basics of MATLAB instead asking such a simple questions.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by