cell配列内でのpolyfitの使い方について

4 views (last 30 days)
flower
flower on 9 May 2023
Commented: flower on 9 May 2023
1×5のcellがあり、その中にそれぞれx行2列のデータが入っています。
xはcellごとにサイズが異なります。
polyfitを使用してそれぞれのcellに格納されている行列に対して、近似直線の傾きと切片を得たいのです。
出力としては1×5のcellでそれぞれに傾きと切片が入っているイメージです。
cellfunを使用するのですが、x行2列を入力引数に入れる書き方が分からずうまくいきません。
どなたかご教示ください。

Answers (1)

Atsushi Ueno
Atsushi Ueno on 9 May 2023
一旦無名関数にcell配列を入力して、その後cell要素をpolyfit関数に入力する際に分けるのが分かり易いと思います。
C = cell(1,5); % 1×5のcell
C = cellfun(@(x) rand(randi(10),2),C,'uni',false) % それぞれx行2列のデータ
C = 1×5 cell array
{5×2 double} {2×2 double} {10×2 double} {7×2 double} {5×2 double}
cellfun(@(x) polyfit(x(:,1),x(:,2),1),C,'uni',false) % 1×5のcell、それぞれに傾きと切片
ans = 1×5 cell array
{[-0.1586 0.5496]} {[2.5811 -1.3494]} {[0.3734 0.2538]} {[-0.0726 0.5822]} {[0.2797 0.2481]}
  1 Comment
flower
flower on 9 May 2023
早速、的確なご回答ありがとうございます。
cellfunと無名関数の書き方がよく理解できました。ありがとうございました。

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!