Combining cell arrays with empty doubles

3 ビュー (過去 30 日間)
Michael Stollenwerk
Michael Stollenwerk 2021 年 1 月 8 日
編集済み: Jan 2021 年 1 月 8 日
Suppose I have two cell arrays, with
a = cell(1,2);
a{1} = 1;
b = cell(1,2);
b{2} = 2;
How can I quickly combine the two to give me
1×2 cell array
{[1]} {[2]}
?
In general, I want to combine several cell arrays of the same dimension, which at a given index either either all have {0×0 double} doubles or at most one of the cell arrays does not have a {0×0 double} double.
Thanks!
Michael

採用された回答

Stephen23
Stephen23 2021 年 1 月 8 日
編集済み: Stephen23 2021 年 1 月 8 日
% slightly more complex example data:
a = {[],1;[],[]};
b = {[],[];2,[]};
c = {[],[];[],NaN};
%
tmp = cat(3,a,b,c);
[~,idp] = sort(cellfun('isempty',tmp),3);
sz = size(tmp);
[idr,idc] = ndgrid(1:sz(1),1:sz(2));
idx = sub2ind(sz,idr,idc,idp(:,:,1));
out = tmp(idx)
out = 2x2 cell array
{0×0 double} {[ 1]} {[ 2]} {[NaN]}

その他の回答 (1 件)

Jan
Jan 2021 年 1 月 8 日
編集済み: Jan 2021 年 1 月 8 日
Or simpler:
a = {1, []};
b = {[], 2};
c = cat(2, a, b);
c = c(~cellfun('isempty', c));
% Or:
fullCell = @(x) x(~cellfun('isempty', x));
c = cat(2, fullCell(a), fullCell(b));

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by