Azzera filtri
Azzera filtri

Find slope across columns of cell

3 visualizzazioni (ultimi 30 giorni)
Carver Nabb
Carver Nabb il 4 Gen 2019
Commentato: Bob Thompson il 7 Gen 2019
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!

Risposta accettata

Bob Thompson
Bob Thompson il 4 Gen 2019
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 Commenti
Carver Nabb
Carver Nabb il 7 Gen 2019
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 il 7 Gen 2019
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.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by