Interweave two arrays in a specific pattern

11 ビュー (過去 30 日間)
Adam Fitchett
Adam Fitchett 2019 年 10 月 30 日
コメント済み: Daniel M 2019 年 10 月 30 日
I have two column vectors each of which is 160 rows long. I want to interweave them such that the first 16 rows of vector 2 are underneath the first 16 rows of vector 1, the second 16 rows of vector 2 are underneath the original second 16 rows of vector 1 and so on. E.g. the vectors are interweaved in alternating blocks of 16. Is there a simple way to do this?

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2019 年 10 月 30 日
編集済み: Fangjun Jiang 2019 年 10 月 30 日
%%
a=(1:32)';
b=(10:10:320)';
%%
c=reshape([a,b],16,[]);
d=c(:,[1:2:end,2:2:end]);
e=d(:)
%% better solution from Guillaume
c=[reshape(a,16,[]);reshape(b,16,[])];
e=c(:)
  2 件のコメント
Guillaume
Guillaume 2019 年 10 月 30 日
編集済み: Guillaume 2019 年 10 月 30 日
May be simpler:
reshape([reshape(a, 16, []); reshape(b, 16, [])], [], 1) %reshape a and b separately into columns of 16 rows. Vertically concatenate the two and reshape back into vector
Fangjun Jiang
Fangjun Jiang 2019 年 10 月 30 日
Apparently, it is better! Thanks!

サインインしてコメントする。


Daniel M
Daniel M 2019 年 10 月 30 日
編集済み: Daniel M 2019 年 10 月 30 日
x = [1:160]';
y = x;
xx = reshape(x,16,[])';
yy = reshape(y,16,[])';
zz = [xx,yy]';
z = zz(:);
  2 件のコメント
Guillaume
Guillaume 2019 年 10 月 30 日
編集済み: Guillaume 2019 年 10 月 30 日
None of the (conjugate!) tranpose are necessary. Simply use zz = [xx; yy]
Daniel M
Daniel M 2019 年 10 月 30 日
details....details...
% edited:
% your starting point, x and y are two column vectors
x = [1:160]';
y = x
%
xx = reshape(x,16,[]); % put into matrix of 16 rows, N columns
yy = reshape(y,16,[]);
zz = [xx,yy]; % vertically concatenate
z = zz(:); % column vector again
Note: the two answers are now identical.

サインインしてコメントする。

カテゴリ

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

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by