Code to assign correlation values from a correlation matrix to a column vector

2 次查看(过去 30 天)
I am trying to create a loop that repeats (iters) times which creates 2 random vectors (v1 and v2) using randn of size (sz) and finds the correlation between those two vectors, and I have to assign the correlation values to a column vector (cors) and plot the histogram of (cors).
My problem is i dont know how to assign correlation values to a column vector. Can anyone help?

回答(1 个)

Turlough Hughes
Turlough Hughes 2023-4-21
You're almost there - use the loop variable, ii, to index into cors as follows:
sz = 1000;
iters = 10000;
cors = zeros(iters,1);
for ii = 1:iters
v1 = randn(sz,1);
v2 = randn(sz,1);
x = corrcoef([v1,v2]);
cors(ii,1) = x(1,2); % <- here
end
histogram(cors,120)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by