필터 지우기
필터 지우기

Insert an array into another array in a specific location.

조회 수: 60 (최근 30일)
Odrisso
Odrisso 2017년 1월 27일
답변: liju Abraham 2019년 2월 12일
Hi, Let I have the following array:
A =[10 20 30];
B = [1 2 3 4 5 6 7 8 9];
Now, I have a number called c, where c can be from 1 to 3.
Now, I want to write a code which can insert values of A into B after every location of C. So, my final output would be for C=3:
F = [1 2 3 10 4 5 6 20 7 8 9 30]
my final output would be for C=1:
F = [1 10 2 20 3 30 4 5 6 7 8 9]
Please let me know, what is the efficient way of coding to implement it?
Thanks

채택된 답변

KSSV
KSSV 2017년 1월 27일
A =[10 20 30];
B = [1 2 3 4 5 6 7 8 9];
C = 3 ;
F = [1 2 3 10 4 5 6 20 7 8 9 30]
iwant = zeros(1,length(B)+length(A)) ;
% get positions to fill A at C
pos = (C+1):(C+1):length(iwant) ;
% other positions
idx = ones(1,length(iwant)) ;
idx(pos) = 0 ;
% fill
iwant(pos) = A ;
iwant(logical(idx)) = B
  댓글 수: 3
KSSV
KSSV 2017년 1월 27일
A =[10 20 30];
B = [1 2 3 4 5 6 7 8 9];
C = 1 ;
F = [1 10 2 20 3 30 4 5 6 7 8 9]
iwant = zeros(1,length(B)+length(A)) ;
% get positions to fill A at C
pos = (C+1):(C+1):length(iwant) ;
pos = pos(1:length(A)) ;
% other positions
idx = ones(1,length(iwant)) ;
idx(pos) = 0 ;
% fill
iwant(pos) = A ;
iwant(logical(idx)) = B
Adam Johnson
Adam Johnson 2018년 4월 15일
Great Answer KSSV, I have a similar issue which I am trying to adapt this code to but so far no luck. I was wondering if you could help. I have a column vector created from the output of a motion sensor showing 1 for motion and 0 for no motion. the vector is large and is only created of numbers 0 & 1 for when motion is detected. I want to input another vector into this vector like you have done above. I have created a vector full of ones to simulate the motion being high for a defined period of time. I would like to as this vector into the motion sensor vector every time it is high and to restart every time it is high.
eg.
Motion = [0 0 0 1 0 0 0 1 0 0 1 1 1 1] Delay = [ 1 1 1 1 ]
my output would be
Output = [0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
so whenever there is a high or 1 in the motion vector the delay is added into it. but is re-triggered when the next 1 occurs.
thanks in advance, Adam

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

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2017년 1월 27일
편집: Andrei Bobrov 2017년 1월 27일
A =[10 20 30];
B = [1 2 3 4 5 6 7 8 9];
c = 2;
n = numel(A);
k = c*n;
B0 = [reshape(B(1:k),c,[]);A];
out = [B0(:).',B(k+1:end)];
Other way
A =[10 20 30];
B = [1 2 3 4 5 6 7 8 9];
c = 3;
n = numel(A);
out = zeros(1,n+numel(B));
out((1:n)*(c+1)) = 1;
out(out > 0) = A;
out(out == 0) = B;
  댓글 수: 2
Odrisso
Odrisso 2017년 1월 27일
Hi, Thanks for the answer. Your answer is fine. However, I put a wrong expression when I said c=3. What I mean, C can be changed. So, C=1 or 2; the code still should work. So, if I place C = 1, then output would be:
F = [1 10 2 20 3 30 4 5 6 7 8 9]
Please let me know, How can I do that.
Andrei Bobrov
Andrei Bobrov 2017년 1월 27일
I'm corrected.

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


liju Abraham
liju Abraham 2019년 2월 12일
I have a similar question:
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
B = [ 10 20 30]
C= 3 % is the position where I want to insert B in A
I = 2 % is the number of times or multiple
output must be:
F = [ 1 2 3 10 20 30 4 5 6 10 20 30 7 8 9 10 11 12 13 14 15]
if C= 2 and I = 4
then, F = [ 1 2 10 20 30 3 4 10 20 30 5 6 10 20 30 7 8 10 20 30 9 10 11 12 13 14 15]

카테고리

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