How do I remove "nested/deeper" layers of a cell?

22 vues (au cours des 30 derniers jours)
lil brain
lil brain le 12 Déc 2022
Modifié(e) : Stephen23 le 13 Déc 2022
Hi,
I have a cell called new_mat that has 2 levels (if I double click on a cell then another cell with the same value opens). Is it possible to remove the last/deepest layer somehow?
Thanks!

Réponses (1)

Jan
Jan le 12 Déc 2022
You have posted a file, which contains the cell matrix called "new_mat". It does not have any second level. Therefore you cannot remove such a level also.
data = load('new_mat.mat')
% data =
% struct with fields:
% new_mat: {5×5 cell}
new_mat = data.new_mat;
  2 commentaires
lil brain
lil brain le 13 Déc 2022
I think I mis-worded my question @Jan. I was wondering if it is possible to not make it so that each cell contains a scalar but rather just the value itelf?
Stephen23
Stephen23 le 13 Déc 2022
Modifié(e) : Stephen23 le 13 Déc 2022
"I was wondering if it is possible to not make it so that each cell contains a scalar but rather just the value itelf?"
What exactly does that mean? By definition, a scalar numeric is just a single value.
It might make sense to you, but you will have to explain using some other words, of what you actually want to achieve (not in terms of concepts that already have meanings in MATLAB and mean basically the same thing).
All cells (except one) of your cell array contain scalar numerics which by definition are single values:
S = load('new_mat.mat')
S = struct with fields:
new_mat: {5×5 cell}
C = S.new_mat
C = 5×5 cell array
{[ 2.9473]} {[ 0.7736]} {[24.7335]} {[-32.1028]} {[ 5.4609]} {[ 7.9357]} {[15.6115]} {[28.3915]} {[ 51.8624]} {[ 1]} {[38.3376]} {[62.5463]} {[35.4955]} {[ 17.6059]} {[ 35.9168]} {[15.0732]} {[24.9668]} {[ 3.2505]} {[-21.6557]} {0×0 double} {[57.9756]} {[49.9486]} {[53.4301]} {[ 45.9361]} {[-17.1092]}
I am guessing that you actually want to create a numeric array from this cell array: this is possible only after replacing the empty cells with some scalar values (e.g. NaN):
C(~cellfun(@isscalar,C)) = {NaN};
M = cell2mat(C)
M = 5×5
2.9473 0.7736 24.7335 -32.1028 5.4609 7.9357 15.6115 28.3915 51.8624 1.0000 38.3376 62.5463 35.4955 17.6059 35.9168 15.0732 24.9668 3.2505 -21.6557 NaN 57.9756 49.9486 53.4301 45.9361 -17.1092
Note that this does not make "...so that each cell contains a scalar but rather just the value itelf" for the simple reason that it is not a cell array and does not have cells. Numeric arrays have elements, but not cells. Perhaps this was the cause of your confusing request.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and 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