Interleaving columns of two cell arrays

7 ビュー (過去 30 日間)
Raphael Chazelle
Raphael Chazelle 2016 年 5 月 19 日
編集済み: the cyclist 2016 年 5 月 19 日
Hey all,
I have two cell arrays:
  • ca = 7x9 cell array where each cell is a block of pixels 64x64x3
  • ca2 = 7x8 cell array where each cell is a block of pixels 64x64x3
I would like to interleave the cell array by columns like so:
  • ca3 = {ca{:,1};ca2{:,1};ca{:,2};ca2{:,2};ca{:,3};ca2{:,3};ca{:,4};ca2{:,4};ca{:,5};ca2{:,5};ca{:,6};ca2{:,6};ca{:,7};ca2{:,7};ca{:,8};ca2{:,8};ca{:,9}}';
I'm sure there has to be a more programmatic way of doing this and in a more extensible fashion(where if I have more then 8/9 columns I wouldn't have to keep adding to the code).
Thanks! Hope this makes sense

採用された回答

Kirby Fears
Kirby Fears 2016 年 5 月 19 日
編集済み: Kirby Fears 2016 年 5 月 19 日
Here's a function I wrote for you. It contains a subfunction to create the interleaving index. Save this to interleaveCells.m and try calling it.
function c = interleaveCells(c1,c2)
% Make index for interleaving cells
ind = interleaveIndex(size(c1,2),size(c2,2));
% Combine cells
c = [c1, c2];
% Interleave cells
c = c(:,ind);
function ind = interleaveIndex(n1,n2)
ind = NaN(1, n1 + n2);
i = 1;
j = 1;
while i<=n1,
ind(i+j-1) = i;
i = i + 1;
if j<=n2,
ind(i+j-1) = n1 + j;
j = j + 1;
end
end
while j<=n2,
ind(i+j-1) = n1 + j;
j = j + 1;
end
Hope this helps.
  2 件のコメント
Raphael Chazelle
Raphael Chazelle 2016 年 5 月 19 日
Works like a charm. Thanks!
Kirby Fears
Kirby Fears 2016 年 5 月 19 日
Great. I am editing in a change that simplifies the subfunction as well.
Cheers.

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

その他の回答 (2 件)

the cyclist
the cyclist 2016 年 5 月 19 日
I think this does what you want
% Your actual data here. (These are empty, but wouldn't be for your application)
ca = cell(7,9);
ca2 = cell(7,8);
% Append extra, empty column of cells, so that we can concatenate below
ca2_tmp = [ca2, cell(7,1)];
% Concatenate top-to-bottom, then reshape
ca3 = reshape([ca;ca2_tmp],7,18);
% Trim spurious final column
ca3(:,end) = [];
  1 件のコメント
Raphael Chazelle
Raphael Chazelle 2016 年 5 月 19 日
This one works! the one below is close but not the desired output!

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


the cyclist
the cyclist 2016 年 5 月 19 日
編集済み: the cyclist 2016 年 5 月 19 日
ca3 = [ca,ca2];
idx = reshape(1:18,[],2)'
ca3 = ca3(:,idx(1:end-1));
EDIT: Fixed this to get the indexing right. Arguably a bit easier than my other solution, but basically works on the same principle.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by