简体   繁体   中英

MATLAB save multiple outputs from a function many times over

In MATLAB, I am trying to create a matrix of the outputs of the built-in function [r, p] = corr(X1,Y1); after using this function on multiple X's and Y's. Then, I would like to consolidate all of the r and p into their respective matrices, R and P. For example, I can do this easily if I only call one output from corr :

R = [corr(X1,Y1), corr(X2,Y2); (...)
     corr(X3,Y3), corr(X4,Y4)];

as corr returns the r value by default. Is there a way to achieve this for p as well? Below is the long way that I do it, I'm just wondering whether there is a shorter and easier method like above.

First find each r and p :

[r1, p1] = corr(X1,Y1);
[r2, p2] = corr(X2,Y2);
[r3, p3] = corr(X3,Y3);
....

Then combine them into the matrix:

R = [r1 r2; (...)
     r3 r4; (...)
     ...];
P = [p1 p2; (...)
     p3 p4; (...)
     ...];

Thanks.

You can try something along the lines of

for i=1:n,
  [R(:,end+1), P(:,end+1)] = corr(X(:,i), Y(:,i));
end

Just make sure that R(:,1) and P(:,1) are sized correctly. Assigning R(:,end+1) and P(:,end+1) will grow R and P automatically, without your having to combine them from temporary variables by hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM