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 Center および File ExchangeData Preprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by