Issues with splitting a matrix into many smaller matrix

1 vue (au cours des 30 derniers jours)
Lachlan Stephens
Lachlan Stephens le 11 Août 2023
Commenté : Stephen23 le 11 Août 2023
I am trying to group my origional matrix (size 11250 x 2) into many smaller matricies, grouped by the value in the first column.
I then want to take the mean and standard deviation of these values to report on the data that I have recieved.
PIN
Unrecognized function or variable 'PIN'.
PIN_SORT = sort(PIN.', 2).'
n = 1
for i = -4.5200:0.04: 0.7200
M{n} = PIN_SORT(find(PIN_SORT(:,1)== i),:);
%disp(M{'i'})
%disp(i)
n = n+1
end
avg{n} = mean(M{n})
stddev{n} = std(M{n})
The code does what I want it to do, for certain values, and will leave an empty spot in the array for the rest.
The two comments were attepmts at trouble shooting, but alas to no success.
Is there a glaringly obvious flaw in my approach or in my code, or is there an oversight on my behalf.

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Août 2023
for i = -4.5200:0.04: 0.7200
The i are floating point values.
M{n} = PIN_SORT(find(PIN_SORT(:,1)== i),:);
You are comparing PIN_SORT(:,1) to a floating point value for bit-for-bit equality. Most values will not be found.
MATLAB does not calculate in decimal. The value -4.5200 cannot be exactly represented in binary double precision. The value 0.04 cannot be exactly represented in binary double precision. The not-exactly-0.04 will be repeatedly added to the original not-exiactly -4.5200 with the representation error for 0.04 accumulating each time.
You should not compare floating point numbers for equality -- except for cases where the values being compared are drawn from the same source. For example, it is valid to test
PIN_SORT(:,1) == min(PIN_SORT(:,1))
because min() will pull out a bit-for-bit copy of one of the values, and you can be sure that bit-for-bit testing will work in that case (provided the array is not all NaN)
  1 commentaire
Walter Roberson
Walter Roberson le 11 Août 2023
T1 = -4.5200:0.04: 0.7200;
T2 = (-452:4:72)/100;
mean(T1 == T2)
ans = 0.3030
Only about 30% of the entries in -4.5200:0.04:0.7200 will be bit-for-bit identical to what you would get if you calculated the corresponding integer divided by 100. The cumulative error on the 0.04 is a problem
You should use ismembertol or discretize

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 11 Août 2023
You need to load the data from csv file into MATLAB first.
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1455237/PIN.CSV');
PIN = T.(4) % pick your column from table T
PIN = 11252×1
NaN NaN -0.0021 -0.0020 -0.0020 -0.0020 -0.0020 -0.0020 -0.0020 -0.0020
  1 commentaire
Lachlan Stephens
Lachlan Stephens le 11 Août 2023
Thanks KSSV, I have done that (just posts the issue in the online compiler), I have attached the fiel so you can see the issue.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by