How to insert a matrix into another matrix

3 ビュー (過去 30 日間)
Mallikarjuna
Mallikarjuna 2013 年 12 月 16 日
回答済み: Azzi Abdelmalek 2013 年 12 月 23 日
let
A = [1 2 3 4 5 6 7 8 9 10 11 12 ------1024]
B = [22 22 22]
I want to generate a matrix C which must have
[22 22 22 1 2 3 4 5 6 7 8 22 22 22 9 10 11 12 13 14 15 16 22 22 22 17 18 19-----1024 22 22 22]
In fact I have a huge data matrix and i want to frame the data by inserting a sync byte after every 1024 bytes
Thanks in advance

回答 (4 件)

Walter Roberson
Walter Roberson 2013 年 12 月 16 日
編集済み: Walter Roberson 2013 年 12 月 16 日
insert_after = 8; %if after every 8 entries
T = reshape(A, insert_after, []);
C = [reshape( [repmat(B(:), 1, size(T, 2)); T], 1, []), B];
This assumes that your A matrix is an exact multiple of the number of entries after which you wish to insert B, and assumes that you want a B on each end of A.
See also the command "buffer" if you have the signal processing toolbox.

Abdullah Khan
Abdullah Khan 2013 年 12 月 16 日
編集済み: Abdullah Khan 2013 年 12 月 16 日
A = [1 2 3 4 5 6 7 8 9 10 11 12 ------1024]
B = [22 22 22]
Try
C= [A B A];

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 16 日
編集済み: Andrei Bobrov 2013 年 12 月 16 日
A = 1:32;
n = 8;
s = numel(B);
B = [2 2 2 ];
C = 1:numel(A)/n*(n+s)+s;
rm = rem(C,n+s);
l = ismember(rm,1:3);
C(l) = B(rm(l));
C(~l) = A;

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 23 日
A = 1:38
B = [22 22 22]
n = 8;
m=numel(A);
idx2=unique([n:n:m m])
idx1=[1 idx2(1:end-1)+1]
out=[B cell2mat(arrayfun(@(ii,jj) [A(ii:jj) B],idx1,idx2,'un',0))]

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by