Swap group of elements in a matrix with specific conditions

2 vues (au cours des 30 derniers jours)
Ted Hein
Ted Hein le 8 Avr 2019
Commenté : Ted Hein le 8 Avr 2019
Currently I have a very complicated set of data with some critical elements messed up by measurement machine. I would like to sort them out and put them in the correct locations. Since my original data size are fairly large, I just make up a similar data as below:
a = [2 1 0.12 0.11; 2 1 0.11 0.13; 2 1 0.04 0.56; 1 1 0.05 0.04; 1 1 0.12 0.65; 1 1 0.11 0.87; 2 3 0.98 0.21; 2 3 0.15 0.22; 2 3 1.21 0.10; 4 3 0.21 0.12; 4 3 0.11 0.25; 4 3 1.05 0.23]
a =
2.0000 1.0000 0.1200 0.1100
2.0000 1.0000 0.1100 0.1300
2.0000 1.0000 0.0400 0.5600
1.0000 1.0000 0.0500 0.0400
1.0000 1.0000 0.1200 0.6500
1.0000 1.0000 0.1100 0.8700
2.0000 3.0000 0.9800 0.2100
2.0000 3.0000 0.1500 0.2200
2.0000 3.0000 1.2100 0.1000
4.0000 3.0000 0.2100 0.1200
4.0000 3.0000 0.1100 0.2500
4.0000 3.0000 1.0500 0.2300
In this matrix, column 1 and 2 are the data cordinate values. We can call it cordinate x and y, corresponding to one set of data. The size of each set of data is the same. The mess-up happens on column 3 and colum 4. Idealy the max value of column 3 for each (x,y) set should be greater than that of column 4. However, due to machine error, some sets of column 4 data stored in column 3 randomly. I tried so many ways to fix this problem. But so far I haven't got a good code. Basically I just use splitapply and findgroups functions since I have (x,y) data. Can you help me to fix this problem? Thanks!
Overall, the key attributes of the data matrix:
1. col1 and col2 are data cordinate values
2. There are same amount of data size for each (col1,col2) set
3. For each set of (col1, col2), the data with higher max value should be stored in col 3. But right now they are randomly stored in col3 and col4.
4. For example, for set (col1=2, col2=1) (the top 3 rows), the higher max value data is in col4. I would like to swap them with col3.
Currently:
2.0000 1.0000 0.1200 0.1100
2.0000 1.0000 0.1100 0.1300
2.0000 1.0000 0.0400 0.5600
What I want:
2.0000 1.0000 0.1100 0.1200
2.0000 1.0000 0.1300 0.1100
2.0000 1.0000 0.5600 0.0400

Réponse acceptée

Guillaume
Guillaume le 8 Avr 2019
Modifié(e) : Guillaume le 8 Avr 2019
Indeed use, findgroup and splitapply:
group = findgroups(a(:, 1), a(:, 2));
groupswap = splitapply(@(c3, c4) max(c4)>max(c3), a(:, 3), a(:, 4), group); %logical array indicating which GROUP to swap. Swap if max of column 4 is greater than max of column 3
toswap = ismember(group, find(groupswap)); %logical array indicating which ROWS to swap
a(toswap, [4, 3]) = a(toswap, [3, 4]) %swap column 3 and 4 for the required groups

Plus de réponses (1)

Alex Mcaulley
Alex Mcaulley le 8 Avr 2019
Try this:
a(:,[3,4]) = sort(a(:,[3,4]),2,'descend');

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by