writing values into nested cell structures.

2 visualizaciones (últimos 30 días)
Yu Takahashi
Yu Takahashi el 5 de Abr. de 2021
Comentada: Yu Takahashi el 6 de Abr. de 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

Respuesta aceptada

Sajid Afaque
Sajid Afaque el 6 de Abr. de 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
  1 comentario
Yu Takahashi
Yu Takahashi el 6 de Abr. de 2021
Thanks for the help!! much more straight forward than I imagined

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Preprocessing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by