cell配列をスカラ​ー配列に直すにはどう​すればいいですか?

81 vues (au cours des 30 derniers jours)
Kohei Yoshino
Kohei Yoshino le 1 Avr 2024 à 16:38
Déplacé(e) : Dyuman Joshi le 5 Avr 2024 à 3:08
1×10 cellの中に要素数の異なるデータが格納されています。これら10個の要素の行平均を算出したいと思っています。
そのため
linspace(leulerX{1}, leulerX{2})
を使用したところ、「入力はスカラーでなければなりません」というエラーが表示されました。
cell配列をスカラーに変換する方法はありますか?
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 1 Avr 2024 à 16:48
Déplacé(e) : Dyuman Joshi le 5 Avr 2024 à 3:08
I am not sure why you are using linspace here.
You can use cellfun to calculate mean of each cell element -
avg = cellfun(@mean, leulerX)
Kohei Yoshino
Kohei Yoshino le 2 Avr 2024 à 17:28
Déplacé(e) : Dyuman Joshi le 5 Avr 2024 à 3:08
Thank you. I understand the use of cellfun. I have listed what I would like to implement in the following questions section.

Connectez-vous pour commenter.

Réponse acceptée

Atsushi Ueno
Atsushi Ueno le 2 Avr 2024 à 15:29
leulerX = cellfun(@(x) rand(randi(100,1,1),1), cell(1,10), 'uni', false) % 実験用サンプルデータ(0-1の乱数)
leulerX = 1x10 cell array
{45x1 double} {89x1 double} {6x1 double} {32x1 double} {14x1 double} {64x1 double} {59x1 double} {66x1 double} {34x1 double} {82x1 double}
rowsizes = cellfun(@size, leulerX, num2cell(ones(1,10))) % 各cell配列要素の行数
rowsizes = 1x10
45 89 6 32 14 64 59 66 34 82
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 1:min(rowsizes) % 10個とも揃っているのは6行目まで
row_slice = cellfun(@(x,k) x(k), leulerX, num2cell(repmat(k,1,10)));
row_mean_value = mean(row_slice) % これら10個の要素の行平均を算出
end
mean_value = 0.4329
mean_value = 0.4435
mean_value = 0.6105
mean_value = 0.6034
mean_value = 0.6154
mean_value = 0.3809
% cell配列をスカラーに変換する方法はありますか?
[A0,A1,A2,A3,A4,A5,A6,A7,A8,A9] = leulerX{:}; % こういう事でしょう
  5 commentaires
Atsushi Ueno
Atsushi Ueno le 4 Avr 2024 à 13:48
 leulerTとqueryの値域は合致していますか?interp1関数がNaNを返すのは外挿法の指定が無い場合です。leulerTよりqueryの値域が広く外挿が発生していると想定しますが、今やりたい事「1周期毎にバラついたデータ数を揃える」に対して外挿は不要だと思います。歩行1周期毎のデータ数を揃えるのに、前後の周期からデータを持ってくる必要があるのでしょうか?
 上記事例では「歩行1周期」を意識してわざわざ0~2πのサンプル点を設けましたが、interp1関数のサンプル点入力を省略し、規定値1:N(サンプル値の長さ)とする事も可能です。従って、下記の構文を使えば外挿は発生せずNaNも表れないでしょう。
period = 2 * pi();
leulerT = cellfun(@(x) (0:period/randi([200 300],1,1):period)', cell(1,10), 'uni', false)
leulerX = cellfun(@sin, leulerT, 'uni', false)
% query = num2cell(repmat(0:fixed_res:period,10,1)',1)
query = cellfun(@(x) 1:length(x)/301:length(x), leulerX, 'uni', false)
% NewleulerX = cellfun(@interp1, leulerT, leulerX, query, 'uni', false) % 変更前
NewleulerX = cellfun(@interp1, leulerX, query, 'uni', false) % 変更後
Kohei Yoshino
Kohei Yoshino le 4 Avr 2024 à 17:02
お返事ありがとうございます。指定していただいた構文で無事にデータ数を揃えることができました。何度もご対応いただきましてありがとうございました。

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 文字と文字列 dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!