Insert an array into another array in a specific location.
82 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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
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;
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!