Clear Filters
Clear Filters

複数データの平均値を​for文にてセルに格​納する方法を教えてく​ださい

17 views (last 30 days)
Kohei Yoshino
Kohei Yoshino on 3 May 2024
Edited: Atsushi Ueno on 3 May 2024
入れ子構造になっているcellデータの行平均データを作成したいと思い、以下のコードを実行しましたが、確認すると明らかに違うデータが格納されてしまいます。
for i = 1:9 % cellデータの変数名(オイラー角X,Y,Z、加速度X,Y,Z、ジャイロX,Y,Zの計9列)
th = sub.Thorax;
for j = 1:numel(th) % 1×26cell(1×1cellの中にはさらに100×9doubleのデータが格納されている)
th_(:,j) = th{j}(:,i); % 各列の周期分を一度th_に格納; th_は100×26doubleのデータ
end
th_mean(:,i) = mean(th{j}(:,i), 2) ; % th_の行平均をth_meanに格納。それを周期分(1:numel(sub.Thorax))繰り返して100×9 doubleのデータを作成したい
end
for文の中でfor文を繰り返す必要があると思うのですが書き方がわかりません。ご教授いただけましたらさいわいです。
  1 Comment
Kohei Yoshino
Kohei Yoshino on 3 May 2024
上の表現では伝わりにくいため、上のコードをfor文使用せずに力技で作成したものを載せます。
th = sub.Thorax;
for j = 1:numel(th) % 1×26cellデータ
th_eulx(:,j) = th{j}(:,1); th_euly(:,j) = th{j}(:,2);th_eulz(:,j) = th{j}(:,3); %オイラー角データ
th_accx(:,j) = th{j}(:,4); th_accy(:,j) = th{j}(:,5);th_accz(:,j) = th{j}(:,6); %加速度データ
th_gyrx(:,j) = th{j}(:,7); th_gyry(:,j) = th{j}(:,8);th_gyrz(:,j) = th{j}(:,9); %ジャイロデータ
end
th_mean = [mean(th_eulx, 2), mean(th_euly, 2), mean(th_eulz, 2), ...
mean(th_accx, 2), mean(th_accy, 2), mean(th_accz, 2), ...
mean(th_gyrx, 2), mean(th_gyry, 2), mean(th_gyrz, 2)]
assignin('base', sprintf('mean_%s_%s_%s', name, seg, speed), th_mean)
for文の中で作成した3行をfor文でth_meanという新しく作成した箱に入れていきたいと考えています。

Sign in to comment.

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 3 May 2024
Edited: Atsushi Ueno on 3 May 2024
 1×26cell(全セル共100×9doubleデータ)
⇒1×1×26cell(全セル共100×9doubleデータ)
⇒100×9×26doubleデータ と変換して、
Z軸方向の平均値を得る方法が分かりやすいと思います。
for文でも出来ますが、変換用の関数を使った方がスマートです。
%% サンプルデータ作成
sub.Thorax = cell(1,26); % 1×26cell
for k = 1:26
sub.Thorax{k} = zeros(100,9); % 1×1cellの中にはさらに100×9doubleのデータが格納されている
end
%% 複数データの平均値をfor文(じゃなく@permute,@cell2mat,@mean)にてセルに格納する方法
% for i = 1:9 % cellデータの変数名(オイラー角X,Y,Z、加速度X,Y,Z、ジャイロX,Y,Zの計9列)
% temp = permute(sub.Thorax,[1,3,2]); % Y軸とZ軸を入れ替え1×1×26cellに変換
% th = cell2mat(temp); % 100×9×26doubleに変換
th = cell2mat(permute(sub.Thorax,[1,3,2]));
% for j = 1:numel(th) % 1×26cell(1×1cellの中にはさらに100×9doubleのデータが格納されている)
%th_(:,j) = th{j}(:,i); % 各列の周期分を一度th_に格納; th_は100×26doubleのデータ
% end
% th_mean(:,i) = mean(th{j}(:,i), 2) ; % th_の行平均をth_meanに格納。それを周期分(1:numel(sub.Thorax))繰り返して100×9 doubleのデータを作成したい
th_mean = mean(th,3);
% end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!