How do I shuffle a set of matrix to find the difference with another matrix?

Hi guys!
So my problem now is that I have two sets of data, both in the form of a matrix of 76x2000. Lets call them A and B.
For one of the matrix (lets say A), I want to relocate at random all the values in the matrix. And then, I will use that random matrix to subtract B and get a difference. I want to do this 1000 times (so basically (1000 different shuffled A) - B) and get an average value of the difference. Can someone please help me put this in a function?
Thank you very much.

 Risposta accettata

A = whatever
B = whatever
m = 1000; % Number of trials
n = numel(A);
C = zeros(1,n);
for k=1:m
C = C + A(randperm(n));
end
result = reshape(C/m,size(A)) - B;

5 Commenti

Hi! Thanks for your help.
When you say A = whatever B = whatever, can I just write like
A = []
B = []?
Thanks!
Sorry I wasn't clear. I am using your variable names. E.g.,
A = one of your 76x2000 matrices
B = your other 76x2000 matrix
no no its fine! thanks for the clarification. I know that its my two matrices, but I meant like in a function, the proper way to write it is
A = []
B = []
right? So this means like if I have 8 sets of A and B, I can use the same function for them all.
Hey!
Sorry, but ignore my previous question. I have fixed that. But regarding the code you wrote, I run the function and I get like 2000 columns worth of result in the command window, but how do I save it into the workplace section?
Thank you!
When calling a function that returns a variable:
myfunction(myinputs etc) % <-- Splash the result to the display screen
x = myfunction(myinputs etc); % <-- Save the result to variable x
Be sure to use the semi-colon at the end of the line to suppress the result splashing to the display screen.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Random Number Generation in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by