Borrar filtros
Borrar filtros

why does data mean(data, 1) give differnt answer form finding mean of individual columns ,like mean_clm = mean(data( : ,1)) ?

1 visualización (últimos 30 días)
why ddoes data mean(data) give differnt answer form finding mean of individual column ,like mean_clm = mean(data( : ,1)) ?
  1 comentario
Stephen23
Stephen23 el 17 de Feb. de 2023
"why ddoes data mean(data) give differnt answer form finding mean of individual column ,like mean_clm = mean(data( : ,1)) ?"
Does it? Lets check:
data = rand(123,7);
M1 = mean(data);
M2 = mean(data(:,1));
isequal(M1(:,1),M2)
ans = logical
1
You should always specify the dimension argument, because the dimension which MEAN() operates along changes depending on the size of the input array. Basically if you want robust code do not use MEAN() with one input.

Iniciar sesión para comentar.

Respuestas (2)

Dyuman Joshi
Dyuman Joshi el 17 de Feb. de 2023
"why does data mean(data, 1) give differnt answer form finding mean of individual columns, like meancl = mean(data( : ,1)) ?"
Because both the commands are different, and thus give different outputs.
Individual column(s) is/are different than a dimension
mean(data(:,k))
This is to calculate the mean of the kth column. k can be a scalar or a vector of values.
Whereas
mean(data,k)
returns the mean along the kth dimension.
Take a look at the documentation of mean to understand more.
  10 comentarios
Dyuman Joshi
Dyuman Joshi el 17 de Feb. de 2023
Looks good to me, @Celestie Gladys
load('imu.mat')
out1 = mean(data1(:,1))
out1 = single 24.0084
out2 = mean(data1);
out2(1)
ans = single 24.0084

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 19 de Feb. de 2023
For future reference:
If mean() detects that the amount of data is below some internal size threshold, then it runs a straight forward internal loop summing the values along the dimension strictly from beginning to end, and then dividing by the size.
However if mean detects that the array is "large enough" it runs the request through a high performance multi-core library. The library might split the data up into pieces and compute the sum of each piece, and then the overall sum. Algebraically the results are the same, but there might be differences in floating point.
Suppose for example that you had an array that contained 1 exactly, then nine zeros, then 10 cases of 1e-17. If you run this forward in order, the 1 exactly carries through the first half. Then you have 1 exactly plus 1e-17, but 1e-17 is too small relative to 1 exactly to make a difference in double precision, so the sum would still be 1 exactly. And that would carry through the rest of the 1e-17, so the overall sum in strict order would be 1 exactly.
Now suppose you split the array into halves and sum each half. The 1 followed by zeros still adds to 1 exactly. But the ten 1e-17 are not negligible compared to each other, so that half would sum to approximately 1e-16. And 1e-16 is not negligible compared to 1 exactly, so when you add the two partial sums the result is not exactly 1.
When you mean() an entire array over the first dimension then the size of the entire array is used to decide whether to use the high performance routines. But when you select a single column and mean that, it is the size of the column by itself that is used, and since that is smaller it could lead to a different decision about whether to use the high performance routines.
In most cases, the difference between these possibilities is pretty small, but in worst case it can be arbitrarily large compared to the magnitude of the values, if there is a lot of accumulated round-off error.
From time to time it can lead to problems, with means that you "know" should be non-negative showing up as slightly negative, which gets to be a problem if you are using sqrt() for example. Different order of addition of floating point numbers can lead to confusing results because of round-off.
This is not the situation you happened to be facing, but Yes, you can end up with different results for the different calls.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by