필터 지우기
필터 지우기

how to insert an element into any index of vector?

조회 수: 55 (최근 30일)
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 5월 25일
댓글: Muhammad Bilal 2022년 12월 19일
CASE 1 :
A = [540]
B = [0,15]
How to insert B(1) as A(1) and B(2) as A(3) ===>>> OUTPUT SHOULD BE LIKE: A = [0,540,15]
Case 2 :
A = [0,540]
B = 15
How to insert B(1) as A(3) ===>>> OUTPUT SHOULD BE LIKE: A = [0,540,15]

답변 (2개)

Ameer Hamza
Ameer Hamza 2018년 5월 25일
편집: Ameer Hamza 2018년 5월 25일
These are very basic concepts in MATLAB vector creation and its indexing. You can go through these links to know how to do these things.
For example in the first case,
A = [540];
B = [0,15];
A = [B(1) A B(2)]
A =
0 540 15
In the second case
A = [0,540];
B = 15;
A = [A B]
A = [A B]
A =
0 540 15
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2018년 5월 26일
There are general methods available. You need to read the documentation at the links I provided in the answer.
George Revell
George Revell 2021년 11월 28일
편집: George Revell 2021년 11월 28일
Excuse me, but your method of creating a collection by defining it in terms of parts of other collections, if you tried to put that in a loooooong forloop, you would end up with an absolutely apauling time complexity to finish execution. While you are techinically correct that your method works, it has such a terrible time complexity that obviously it could only be used for relatively small collections. if instead one of the arrays had a dimension beyond n = 10000, well good luck running this algorithm...

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


Iskandar Ars
Iskandar Ars 2021년 12월 21일
v = 1:10; %new vector
i = 5; % index to insert
b = 15; % the number you want to insert
v = [v(1:i-1) b v(i:end)] % insert
v = 1×11
1 2 3 4 15 5 6 7 8 9 10

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by