MATLAB: Help with basic code, pls

random number generator

Hi everyone, I need to write a code that do as follow:
1. Calculate the average of vectors a1,a2 (they are at the some size)
2. Calculate the difference between the averages (|<a1> -<a2>|)
3. Build a1-a2 matrix (Nx2)
4. Randomly pick N numbers from the matrix to one column and the others to the second
calculate the average and the difference again
5. Shuffle and repeat for 1000 times
6. Check if what we got in step 2 is within the higher 5%
7. If the answer is yes write: '1' otherwise '0'.
I attach the code i wrote and needs help to continue.
function [logic] = calc(a1,a2)
% a1,a2 are column vetors
mean_a1 = mean(a1);
mean_a2 = mean(a2);
avg_diff = abs(mean_a1-mean_a2);
matrix = [a1,a2];
end
Thank you very much!

Best Answer

try randperm.
for i=1:1000
p = randperm(2 * size);
martix = [ rowmatrix(p(1:size)), rowmatrix(p(size+1:end)) ];
mean_row1 = mean(martix(1,:));
mean_row2 = mean(martix(2,:));
avg_diff_matrix(i) = abs(mean_row1-mean_row2);
end
Related Question