Insert an array into another array in a specific location.

79 visualizaciones (últimos 30 días)
Odrisso
Odrisso el 27 de En. de 2017
Respondida: liju Abraham el 12 de Feb. de 2019
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

Respuesta aceptada

KSSV
KSSV el 27 de En. de 2017
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 comentarios
KSSV
KSSV el 27 de En. de 2017
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 el 15 de Abr. de 2018
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

Iniciar sesión para comentar.

Más respuestas (2)

Andrei Bobrov
Andrei Bobrov el 27 de En. de 2017
Editada: Andrei Bobrov el 27 de En. de 2017
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 comentarios
Odrisso
Odrisso el 27 de En. de 2017
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.

Iniciar sesión para comentar.


liju Abraham
liju Abraham el 12 de Feb. de 2019
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]

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by