Find slope across columns of cell

3 次查看(过去 30 天)
I have a 220x4 cell (a) consisting of numerical data and would like to find the (linear) slope across the four values in row one, then the four values in row two, and so on, so that I would end up with 220 different slopes that I could then save. Thanks in advance!

采纳的回答

Bob Thompson
Bob Thompson 2019-1-4
How are you organizing the data into ordered pairs? I'm assuming that by 'numerical data' you mean you have cells that contain individual doubles.
data = cell2mat(a);
for i = 1:size(a,1)
slope(i) = polyfit(x_vals,data(i,:),1);
end
  6 个评论
Carver Nabb
Carver Nabb 2019-1-7
This worked great, Bob, thank you. However, now that I have NaN values, the polyfit function returns only NaN for P and S when the row contains a NaN value. Is there a way to ignore the NaNs?
Bob Thompson
Bob Thompson 2019-1-7
for i = 1:size(a,1);
pairs = [];
pairs(:,1) = counter;
pairs(:,2) = data(i,:);
pairs = pairs(~isnan(pairs(:,2),:);
slope(i) = polyfit(pairs(:,1),pairs(:,2),1);
end
This might not quite do it, the logic indexing to remove NaNs might be slightly off, but it should get you going at least.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by