Effacer les filtres
Effacer les filtres

Assign values in the cell

21 vues (au cours des 30 derniers jours)
Turbulence Analysis
Turbulence Analysis le 16 Juil 2021
Commenté : Rik le 25 Avr 2022
Hi,
I have the 72 x 1 cell named output 1 (attached here).
As shown in the attachment, some cells got empty matrix, I intend to add zero values in the manner of 8x1 into all the all empty cells ..
Any idea how to assign this .. ??

Réponse acceptée

Rik
Rik le 16 Juil 2021
Modifié(e) : Rik le 16 Juil 2021
You mean like this?
output1={[1 2;3 4],[]};
L=cellfun(@isempty,output1);
output1(L)={zeros(8,1)};
output1
output1 = 1×2 cell array
{2×2 double} {8×1 double}
Note that cellfun has a legacy mode, which is often much faster, but is limited in terms of what data types will work.
  5 commentaires
Amir Pasha Zamani
Amir Pasha Zamani le 25 Avr 2022
It only works for a single element of the cell.
I need to pre-assign values (let's say zeros(10,10)) to all cell elemenst.
Rik
Rik le 25 Avr 2022
@Amir Pasha Zamani L can be a logical array with as many elements as output1 (or fewer). So can you explain what you mean?
To pre-allocate the array you describe I suggest using repmat or repelem:
YouWant=repmat({zeros(10,10)},2,3)
YouWant = 2×3 cell array
{10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double}

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by