Insert an array into another array in a specific location.
79 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Odrisso
am 27 Jan. 2017
Beantwortet: liju Abraham
am 12 Feb. 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
0 Kommentare
Akzeptierte Antwort
KSSV
am 27 Jan. 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 Kommentare
KSSV
am 27 Jan. 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
am 15 Apr. 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
Weitere Antworten (2)
Andrei Bobrov
am 27 Jan. 2017
Bearbeitet: Andrei Bobrov
am 27 Jan. 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 Kommentare
liju Abraham
am 12 Feb. 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]
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!