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

81 次查看(过去 30 天)
Kohei Yoshino
Kohei Yoshino 2024-4-1,16:38
移动Dyuman Joshi 2024-4-5,3:08
1×10 cellの中に要素数の異なるデータが格納されています。これら10個の要素の行平均を算出したいと思っています。
そのため
linspace(leulerX{1}, leulerX{2})
を使用したところ、「入力はスカラーでなければなりません」というエラーが表示されました。
cell配列をスカラーに変換する方法はありますか?
  2 个评论
Dyuman Joshi
Dyuman Joshi 2024-4-1,16:48
移动:Dyuman Joshi 2024-4-5,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 2024-4-2,17:28
移动:Dyuman Joshi 2024-4-5,3:08
Thank you. I understand the use of cellfun. I have listed what I would like to implement in the following questions section.

请先登录,再进行评论。

采纳的回答

Atsushi Ueno
Atsushi Ueno 2024-4-2,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 个评论
Atsushi Ueno
Atsushi Ueno 2024-4-4,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 2024-4-4,17:02
お返事ありがとうございます。指定していただいた構文で無事にデータ数を揃えることができました。何度もご対応いただきましてありがとうございました。

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 文字と文字列 的更多信息

Community Treasure Hunt

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

Start Hunting!