Add element inside cell with combination

1 回表示 (過去 30 日間)
NA
NA 2022 年 7 月 22 日
コメント済み: NA 2022 年 7 月 22 日
I have a cell A.
A = {[],[1,2],[3,4],[5,6]}
I want to add each element inside the cell A in a combination.
This is the result I want
1+3+5 ---> 9
1+3+6 ---> 10
1+4+5 ---> 10
1+4+6 ---> 11
2+3+5 ---> 10
2+3+6 ---> 11
2+4+5 ---> 11
2+4+6 ---> 12
result = [9;10;10;11;10;11;11;12]

採用された回答

KSSV
KSSV 2022 年 7 月 22 日
A = {[],[1,2],[3,4],[5,6]} ;
A = A(~cellfun('isempty',A)) ; % REmove empty cells
[I{1:numel(A)}] = ndgrid(A{:}) ;
thesum = I{1} ;
for i = 2:numel(A)
thesum = thesum+I{i} ;
end
iwant = thesum(:)
iwant = 8×1
9 10 10 11 10 11 11 12

その他の回答 (1 件)

David Hill
David Hill 2022 年 7 月 22 日
[a,b,c]=meshgrid(A{2},A{3},A{4});
s=sum([a(:),b(:),c(:)],2);
  1 件のコメント
NA
NA 2022 年 7 月 22 日
Thank you for your time.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by