フィルターのクリア

Increase the number of elements inside a cell

1 回表示 (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 6 月 24 日
回答済み: Stephen23 2023 年 6 月 24 日
Hi. I have this cell:
A = {'5';'11'};
B = {'7';'19'};
out = [A,B]; % cell 2x2
I should go from an 'out' cell 2x2 to an 'out' cell 2x4, where the missing columns (columns 3 and 4) are null cells:
empty_cell = cell(2,1);
The total number of columns in the 'out' cell should be imposed by:
parameter = 4;
So, the end result I want to get must be this:
empty_cell = cell(2,1);
out = [A,B,empty_cell,empty_cell]; % cell becoming 2x4 (where 4 is 'parameter')

採用された回答

Stephen23
Stephen23 2023 年 6 月 24 日
No loop, no concatenation required:
A = {'5';'11'};
B = {'7';'19'};
out = [A,B]; % cell 2x2
parameter = 4;
out(:,end+1:parameter) = {[]}
out = 2×4 cell array
{'5' } {'7' } {0×0 double} {0×0 double} {'11'} {'19'} {0×0 double} {0×0 double}

その他の回答 (1 件)

Deep
Deep 2023 年 6 月 24 日
You can simply add empty columns in a loop.
Given out and parameter are defined (out can be a cell of any shape, need not be 2x2),
% Calculate the number of empty columns to be added
num_empty_cols = parameter - size(out, 2);
Then, create an empty cell array with the same number of rows as 'out'
empty_cell = cell(size(out, 1), 1);
Finally add these cells in a simple loop:
for k = 1:num_empty_cols
out = [out, empty_cell];
end
Hope this helps!
  1 件のコメント
Deep
Deep 2023 年 6 月 24 日
You can also directly add a single cell without a loop as Dyuman suggested.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by