writing values into nested cell structures.

조회 수: 1 (최근 30일)
Yu Takahashi
Yu Takahashi 2021년 4월 5일
댓글: Yu Takahashi 2021년 4월 6일
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
  댓글 수: 1
Yu Takahashi
Yu Takahashi 2021년 4월 6일
Thanks for the help!! much more straight forward than I imagined

댓글을 달려면 로그인하십시오.

추가 답변 (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