I get different results for the product of two matrices?

3 Ansichten (letzte 30 Tage)
mehra am 9 Nov. 2022
Verschoben: Matt J am 9 Nov. 2022
Hello
For my u.mat and w.mat data sets I need to do a calculation to find:
u-mean(u) and w-mean(w) and then find the mean of their products.
but when I define this calculation in one line (as in results(2,np)) it gives different results from when I define u_differ and w_differ separately, and then find the mean of their product (results(1,np)). Since I will have some other calculations I prefer to make it short. So I would like to know what is wrong with results(2,np) line that gives different results from results(1,np) although they're the same definition in fact
results=zeros(2,2);
for np=1:2;
u_differ(:,np)=u(:,np)-mean(u(:,np));
w_differ(:,np)=w(:,np)-mean(w(:,np));
results(1,np)=mean(u_differ(:,np).*w_differ(:,np));
results(2,np)=mean(u(:,np)-mean(u(:,np)).*w(:,np)-mean(w(:,np)));
end
0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Marcel Kreuzberg am 9 Nov. 2022
Verschoben: Matt J am 9 Nov. 2022
change results(2,np)=mean(u(:,np)-mean(u(:,np)).*w(:,np)-mean(w(:,np)));
to
results(2,np)=mean( (u(:,np)-mean(u(:,np))) .* (w(:,np)-mean(w(:,np))) );
1 KommentarKeine anzeigenKeine ausblenden
mehra am 9 Nov. 2022
Verschoben: Matt J am 9 Nov. 2022
so the problem was only parantheses! :) Thank you

Melden Sie sich an, um zu kommentieren.

Kategorien

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by