How to Calculate linear trend for each column of a matrix

조회 수: 17 (최근 30일)
Shakir Hussain
Shakir Hussain 2018년 11월 23일
답변: Star Strider 2018년 11월 23일
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

채택된 답변

Star Strider
Star Strider 2018년 11월 23일
Try this:
[D,S] = xlsread('test.xlsx');
B = [(1:size(D,1))', ones(size(D,1),1)] \ D
producing:
B =
-0.7307 -0.6702 -0.5743 -0.5264 -0.3016
38.1515 33.7212 35.0423 35.3009 35.6209
The first row are the slopes and the second row are the intercepts. The columns are for ‘a’ through ‘e’ respectively.

추가 답변 (1개)

Luna
Luna 2018년 11월 23일
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
Shakir Hussain
Shakir Hussain 2018년 11월 23일
편집: Shakir Hussain 2018년 11월 23일
dear luna, thanks for your response
yes you rightly assumed it. Could we deal it a matlab matrix without having column names? and have R and P value of all Y columns.
how do you think about this method https://www.mathworks.com/matlabcentral/fileexchange/46363-trend?
Luna
Luna 2018년 11월 23일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by