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

2 views (last 30 days)
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?

Answers (1)

Turlough Hughes
Turlough Hughes on 21 Apr 2023
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)

Products

Community Treasure Hunt

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

Start Hunting!