Insert an array into another array in a specific location.

82 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by