Azzera filtri
Azzera filtri

Increase the number of elements inside a cell

1 visualizzazione (ultimi 30 giorni)
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')

Risposta accettata

Stephen23
Stephen23 il 24 Giu 2023
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}

Più risposte (1)

Deep
Deep il 24 Giu 2023
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 Commento
Deep
Deep il 24 Giu 2023
You can also directly add a single cell without a loop as Dyuman suggested.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by