Filter löschen
Filter löschen

Grouping and averaging values within a matrix

2 Ansichten (letzte 30 Tage)
JIAN-HONG YE ZHU
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
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?
JIAN-HONG YE ZHU
JIAN-HONG YE ZHU am 5 Dez. 2022
The tolerance criteria is 0.01. My matrix contains values from -1.6 to 1.3 but the posted matrix only goes from -1.6 to -1.59 since there are too many data points.
So I would like all row values close to -1.6 to be averaged and give my one row containing that, same with -1.59, -1.58, -1.57, -1.56 and so on.
I included the variable since I might not be explaning myself right.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kevin Holly
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
m = 27×3
-1.6000 -0.5227 1.6750 -1.6000 -0.5188 1.6750 -1.6000 -0.5227 1.6750 -1.6000 -0.5511 1.6660 -1.6000 -0.5472 1.6660 -1.6000 -0.5433 1.6660 -1.6000 -0.5394 1.6660 -1.6000 -0.5355 1.6660 -1.6000 -0.5082 1.6660 -1.6000 -0.5043 1.6660
unique_values = unique(m(:,1))
unique_values = 2×1
-1.6000 -1.5900
for ii = 1:length(unique_values)
logical_array = m(:,1) == unique_values(ii);
new(ii,:) = mean(m(logical_array,:));
end
new
new = 2×3
-1.6000 -0.5323 1.6691 -1.5900 -0.5318 1.6678

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by