Find slope across columns of cell

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!

 Respuesta aceptada

Bob Thompson
Bob Thompson el 4 de En. de 2019

0 votos

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 comentarios

Yes, the cells have individual doubles. I am unsure exactly how to organize the data into ordered pairs.
The data currently just looks like:
110 182 176 100
185 273 192 284
294 135 323 436...
...
...and so on (220x4)
I tried
counter = 1:4
to use as the x axis data. Would that work?
Carver Nabb
Carver Nabb el 6 de En. de 2019
Hi,
I have tried converting the cell to a matrix however I am met with the error "Dimensions of arrays being concatenated are not consistent". Some of the elements in my 220x4 cell are "[]" (data does not exist for those points). Would this be why this is occuring?
Bob Thompson
Bob Thompson el 7 de En. de 2019
Yes, the empty cells are why that is not working. You can either replace the empty cells with NaN values, or leave the array as cells. Either works, I just usually avoid using cells unless I have data that is more complex than doubles.
Check this link. I know it's about strings, but it will work for replacing empty doubles as well.
Using counter = 1:4 should be fine for x data. You may have problems when you reach your NaN values though, as you don't technically have the same size data sets.
Carver Nabb
Carver Nabb el 7 de En. de 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 el 7 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de En. de 2019

Comentada:

el 7 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by