Dividing data set into subsets and plot them on same graph, with individual trend lines
4 次查看(过去 30 天)
显示 更早的评论
I have a file, file.dat. it have over 40,000 entries (1 column x 40,000 rows). i divided the whole data into 6 equal parts (columns) by using the code below:
%V1 is my data,
Z1 = 6*fix(numel(V1)/6); %numel(V1) counts number of elements in V1. it is numeL, not 1(one); fix(A) will round the number to nearest integer (floor)
M1 = reshape(V1(1:Z1),[],6);
i want to plot each of the six parts (columns) on the same figure one after other(simply figure is divided into 6 equal parts) and want to draw trend line for each part separately. I also want to see the equation of each trend line. Slop of each trend line is important to me.
0 个评论
回答(2 个)
Star Strider
2018-2-9
If you have the Statistics and Machine Learning Toolbox, using the lsline (link) function is likely easiest:
figure(1)
AxH = axes('NextPlot','add')
for k1 = 1:size(M1,2)
scatter((1:size(M1,1)), M1(:,k1))
end
hl = lsline;
for k1 = 1:numel(hl)
B = [ones(size(hl(k1).XData(:))), hl(k1).XData(:)]\hl(k1).YData(:);
Slope(k1) = B(2);
Intercept(k1) = B(1);
end
Slope
Intercept
The ‘Slope’ vector are in the order the data are plotted, so ‘Slope(1)’ is the slope of ‘M1(:,1)’, and so for the others.
0 个评论
Jos (10584)
2018-2-9
% create artificial data
N = 6 ; % number of data sets
M = arrayfun(@(k) polyval([randi(10) 2*k], 1:10), 1:N, 'un',0) ;
M = cat(1,M{:}).' ;
M = M + randi(10,size(M)) - 5 ; % add a little noise
% engine
ph = plot(M,'o') % plot at once, each column is a separate object
% fit the lines through each column
p = arrayfun(@(k) polyfit(1:size(M,1), M(:,k)',1),1:size(M,2), 'un', 0)
lh = cellfun(@(c) refline(c(1), c(2)), p) % plot the lines, retrieve the handles
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!