フィルターのクリア

Interleaving arrays with different sizes

1 回表示 (過去 30 日間)
Matt Holm
Matt Holm 2016 年 11 月 13 日
回答済み: Jos (10584) 2017 年 1 月 5 日
So far i have done this, and it works for arrays of the same size:
function output = interleave(x,y)
output(1:2:6)=x;
output(2:2:6)=y;
end
so if x = [[1 2 3] and y= [10 11 12]) the output is [1 10 2 11 3 12]
but how would i alter the function for arrays of differen sizes
i.e. if x= [1 2] and y [-1 -2 -3] the output is [1 -1 2 -2 -3]
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 13 日
How would you know how to de-interleave them afterwards?
Matt Holm
Matt Holm 2016 年 11 月 13 日
Im not sure how to de-interleave them aha

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

回答 (2 件)

Marco Morganti
Marco Morganti 2017 年 1 月 5 日
Hi Matt,
hope this still can be helpful:
function output = interleave(x,y)
lx = numel(x);
ly = numel(y);
l = lx + ly;
i = 1;
j = 1;
k = 1;
output = zeros(1,l);
while (i <= l)
if (j <= lx)
output(i) = x(j);
j = j+1;
i = i+1;
end
if (k <= ly)
output(i) = y(k);
k = k+1;
i = i+1;
end
end
This function is of course much longer than the one you suggested, however it should be able to handle more general cases.

Jos (10584)
Jos (10584) 2017 年 1 月 5 日
x = 1:5
y = 10:10:30
xy = [x y]
[~, si] = sort([1:numel(x) 1:numel(y)])
result = xy(si)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by