writing values into nested cell structures.

1 次查看(过去 30 天)
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

采纳的回答

Sajid Afaque
Sajid Afaque 2021-4-6
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by