Help with basic code, pls

1 次查看(过去 30 天)
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!
  2 个评论
madhan ravi
madhan ravi 2018-11-11
post an example of your desired output
Nushi Twelve
Nushi Twelve 2018-11-11
编辑:Nushi Twelve 2018-11-11
for example if the result is true: logic = 1 ,else logic = 0.

请先登录,再进行评论。

采纳的回答

HilaN
HilaN 2018-11-13
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

更多回答(1 个)

TADA
TADA 2018-11-11
n = length(a1);
allItems = [a1(:);a2(:)];
% this line of code chooses n random numbers from your two vectors
rearranged = sortrows([allItems, rand(n*2, 1)], 2);
b1 = rearranged(1:n, 1);
b2 = rearranged(n+1:end, 1);
  1 个评论
TADA
TADA 2018-11-11
If in this assignment you MUST use an Nx2 matrix you can always randomize the order of the matrix linear indices instead of the vector itself...
n = length(a1);
allItems = [a1(:),a2(:)];
% these are the linear indices of the matrix allItems
indices = 1:2*n;
% this randomizes the indices and chooses n random items
rearrangedIndices = sortrows([indices(:), rand(n*2, 1)], 2);
b1 = allItems(rearrangedIndices(1:n,1));
b2 = allItems(rearrangedIndices(n+1:end,1));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by