how to insert value of array to other array

조회 수: 2 (최근 30일)
Shehab Tarek
Shehab Tarek 2020년 5월 26일
댓글: Walter Roberson 2020년 5월 27일
hi guys
i have array
rr=[1,2,3,4,5]
i want to insert
y=[5,6,1]
on it to be
rr=[1,2,3,4,5,5,6,1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
the second question insert one element not array
for example
y=2;
rr=[1,2,3,4,5]
rr=[1,2,3,4,5,2]

채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 26일
horzcat(). [list of values] is also horzcat()
  댓글 수: 2
Shehab Tarek
Shehab Tarek 2020년 5월 26일
Walter Roberson
Walter Roberson 2020년 5월 27일
y=2
rr=[1,2,3,4,5]
horzcat(rr,y)
y =
2
rr =
1 2 3 4 5
ans =
1 2 3 4 5 2
Note that this requires that your inputs are both row vectors -- or at least that whatever they are has the same number of rows.
If you are adding new rows to an array see vertcat(), which requires that whatever the inputs are has the same number of columns as each other.
If you do not know ahead of time whether the inputs are row vectors or column vectors or that they are the same as each other , then provided they are both vectors, you can use
newrr = rr;
newrr(end+length(y)) = y;
This will have the same orientation as rr has.

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

추가 답변 (0개)

카테고리

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