pictorexcrucia
pictorexcrucia

Reputation: 179

Using polyfit with plotmatrix

I have a matrix of subplots I created with plotmatrix, and want to add lines of best fit for each one, but can't figure out how to do it. Is there a way to apply polyfit to each subplot separately?

Here is a simplified example of what I have so far

x = randn(50,3);
y = x*[-1 2 1;2 0 1;1 -2 3;]';
[H,AX,BigAx,P,PAx] = plotmatrix(x,y);

I think I need to learn how to index over the matrix of subplots somehow.

Upvotes: 1

Views: 504

Answers (1)

m.s.
m.s.

Reputation: 16324

I don't know how to overlay an existing matrix produced by plotmatrix, but you can create the matrix yourself and overlay each subplot with the results from polyfit:

figure;
x = randn(50,3);
y = x*[-1 2 1;2 0 1;1 -2 3;]';

degree=4;

rows = size(x,2);
cols = size(y,2);

for k=1:rows
    for m=1:cols
        subplot(rows, cols, (k-1)*rows+m);
        hold all;
        scatter(x(:,k),y(:,m),'.');
        p = polyfit(x(:,k),y(:,m),degree);
        x_p = linspace(min(x(:,k)), max(x(:,k)));
        y_p = polyval(p,x_p);
        plot(x_p, y_p,'LineWidth',2);
        hold off;
    end
end

enter image description here

Upvotes: 1

Related Questions