Grouping and averaging values within a matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JIAN-HONG YE ZHU
am 5 Dez. 2022
Kommentiert: JIAN-HONG YE ZHU
am 5 Dez. 2022
Hello, I have a matrix 305109x3 and I want to reduce its size by averaging similar values within the matrix.
The matrix data varies from -1.6 to 1.3 on column one and I want to for example take all the values close to -1.6 on column one and average them.
This is a section of my matrix, column 1 2 3 correspond to x y z data points:
-1.60120522975922 -0.522726416587830 1.67500007152557
-1.60120522975922 -0.518805325031281 1.67500007152557
-1.59728419780731 -0.522726416587830 1.67500007152557
-1.59650182723999 -0.551117777824402 1.66600012779236
-1.59650182723999 -0.547217786312103 1.66600012779236
-1.59650182723999 -0.543317794799805 1.66600012779236
-1.59650182723999 -0.539417743682861 1.66600012779236
-1.59650182723999 -0.535517752170563 1.66600012779236
-1.59650182723999 -0.508217751979828 1.66600012779236
-1.59650182723999 -0.504317700862885 1.66600012779236
-1.59642553329468 -0.564169049263001 1.67000007629395
-1.59642553329468 -0.560259699821472 1.67000007629395
-1.59642553329468 -0.528984725475311 1.67000007629395
-1.59642553329468 -0.525075435638428 1.67000007629395
-1.59642553329468 -0.513347327709198 1.67000007629395
-1.59336316585541 -0.522726416587830 1.67500007152557
-1.59260189533234 -0.566717803478241 1.66600012779236
-1.59260189533234 -0.555017769336700 1.66600012779236
-1.59260189533234 -0.551117777824402 1.66600012779236
-1.59260189533234 -0.547217786312103 1.66600012779236
-1.59260189533234 -0.539417743682861 1.66600012779236
-1.59260189533234 -0.531617760658264 1.66600012779236
-1.59260189533234 -0.508217751979828 1.66600012779236
-1.59260189533234 -0.504317700862885 1.66600012779236
-1.59251618385315 -0.525075435638428 1.67000007629395
-1.59251618385315 -0.517256677150726 1.67000007629395
-1.59251618385315 -0.513347327709198 1.67000007629395
So in this case I would like to average all the values close to -1.6 and all the values close to -1.59.
So instead of having the above 27x3 matrix, I would have a 2x3 matrix.
For reference the 2x3 matrix I would like after doing that would be:
-1.60121 -0.52077 1.675
-1.58829 -0.5472 1.678788
So, for the whole matrix, I would have -1.6, -1.59, -1.58 all up to 1.3 and want to be able to identify numbers close to those and average them, if that make sense?
2 Kommentare
Dyuman Joshi
am 5 Dez. 2022
How do you define close? What is the tolerance criteria? -1.6 +- 0.1?
"So in this case I would like to average all the values close to -1.6 and all the values close to -1.59."
Do you mean 1.3, instead of -1.59?
Akzeptierte Antwort
Kevin Holly
am 5 Dez. 2022
m = [-1.60120522975922 -0.522726416587830 1.67500007152557
-1.60120522975922 -0.518805325031281 1.67500007152557
-1.59728419780731 -0.522726416587830 1.67500007152557
-1.59650182723999 -0.551117777824402 1.66600012779236
-1.59650182723999 -0.547217786312103 1.66600012779236
-1.59650182723999 -0.543317794799805 1.66600012779236
-1.59650182723999 -0.539417743682861 1.66600012779236
-1.59650182723999 -0.535517752170563 1.66600012779236
-1.59650182723999 -0.508217751979828 1.66600012779236
-1.59650182723999 -0.504317700862885 1.66600012779236
-1.59642553329468 -0.564169049263001 1.67000007629395
-1.59642553329468 -0.560259699821472 1.67000007629395
-1.59642553329468 -0.528984725475311 1.67000007629395
-1.59642553329468 -0.525075435638428 1.67000007629395
-1.59642553329468 -0.513347327709198 1.67000007629395
-1.59336316585541 -0.522726416587830 1.67500007152557
-1.59260189533234 -0.566717803478241 1.66600012779236
-1.59260189533234 -0.555017769336700 1.66600012779236
-1.59260189533234 -0.551117777824402 1.66600012779236
-1.59260189533234 -0.547217786312103 1.66600012779236
-1.59260189533234 -0.539417743682861 1.66600012779236
-1.59260189533234 -0.531617760658264 1.66600012779236
-1.59260189533234 -0.508217751979828 1.66600012779236
-1.59260189533234 -0.504317700862885 1.66600012779236
-1.59251618385315 -0.525075435638428 1.67000007629395
-1.59251618385315 -0.517256677150726 1.67000007629395
-1.59251618385315 -0.513347327709198 1.67000007629395];
m(:,1) = round(m(:,1),2);
m
unique_values = unique(m(:,1))
for ii = 1:length(unique_values)
logical_array = m(:,1) == unique_values(ii);
new(ii,:) = mean(m(logical_array,:));
end
new
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!