writing values into nested cell structures.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Yu Takahashi
le 5 Avr 2021
Commenté : Yu Takahashi
le 6 Avr 2021
Dear members
I am having trouble writing values into nested cell structures.
Current dataset : 30*1 cell array A, in which each individual cell contained either 3 or 5 numbers. For example, A(1,1) =[3 5 4], A(2,1) = [3 1 7 0 1] …etc.
The plan is to sequentially add up numbers in the A cell through the following steps:
step 1: adding from the first to the first (i.e., 3)
step 2: adding form the first to the second (i.e., 3+5 = 8)
step 3: adding from the first to the third (i.e., 3+5+4 =12)
step 4: place them in one single array B(1,2) = [3 8 12]
I was thinking about something like the following(forgot to post this yesterday), but kept seeing "Cell contents reference from a non-cell array object," thinking that I must have violated some rules for using nested cell structures.
for r =1:30
if numel(A(r))==3 %identify the number of elements as either 3 or 5
for i= 1:3 %sum them setp by step
B{r} = sum(A{r}(1:i)); %
end
elseif numel(A(r))==5
for i= 1:5
B{r} = sum(A{i}(1:i));
end
end
end
Some visual aid:
Any comments or answers whould be appreciated!
Thanks in advance
0 commentaires
Réponse acceptée
Sajid Afaque
le 6 Avr 2021
try this might help
% example of A
A{1,1} =[3 5 4];
A{2,1} = [3 1 7 0 1] ;
A{3,1} = [3 1 7 ];
%MAIN PART IS BELOW
for i = 1 : length(A)
current_value = A{i};
sum = 0;
output = [];
for j = 1 : numel(current_value)
sum = sum + current_value(j);
output = [ output sum];
end
%store how ever you like it in B
B{i,1} = output;
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Preprocessing 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!