How to Calculate linear trend for each column of a matrix
Mostrar comentarios más antiguos
Can we calculate the linear trend of each column of a matrix without drawing any line,
For example I want to calculate the linear trend for the column a,b,c,d,e of the attached data
Thank you for any tips or help
Respuesta aceptada
Más respuestas (1)
Luna
el 23 de Nov. de 2018
Hi,
I think you can use polyfit simply for this problem.
Here is an example code. You can apply it for the other columns aswell.
[num,txt,raw] = xlsread('test.xlsx');
columnNames = txt(1,:);
columnNames(1) = [];
for i = 1:numel(columnNames)
colData.(columnNames{i}) = num(:,i);
end
x = 1:12;
linearCoefficients = polyfit(x,colData.a',1); % First degree linear fit
yFit = polyval(linearCoefficients, x);
% Apply it for colData.b and c .. so on.
%% To see data and line
figure;
plot(x,colData.a','bo');
hold on;
plot(x,yFit,'-r');
3 comentarios
Luna
el 23 de Nov. de 2018
Btw, I assumed your x data from 1 to 12. You can choose any other column as reference.
Shakir Hussain
el 23 de Nov. de 2018
Editada: Shakir Hussain
el 23 de Nov. de 2018
Luna
el 23 de Nov. de 2018
Ah, that columnNames I just only used to extract the xls file easily.
You can always use below xlsread function outputs as you wish.
num gives you the only numerical matrix, txt only gives you the non-numeric text values, raw is the whole data as a cell array.
just get num and take each column of it one by one.
[num,txt,raw] = xlsread('test.xlsx')
R = num(:,1) % first column
P = num(:,2) % second colum, etc...
According to that function in FileExchange, I haven't run it. You can try that also and compare the results with polyfit.
Categorías
Más información sobre Data Preprocessing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!