Merge rows based on conditions in the coulmns

16 views (last 30 days)
Hamid Basiri
Hamid Basiri on 17 May 2022
Commented: Hamid Basiri on 8 Jun 2022 at 2:42
Hello,
In my simulations two particles (named 13 and -13) are generated and hit these four planes and the information of position (x,y,z) and direction (Vx,Vy,Vz), energy and the event number (I have 500,000 events) are recorded for each as dmp.txt. I am going to sort these data in a way I can have one row for each event as shown in the figure that means if the first column and the last column of each row are same, the rows must br merged into one row . As the data is big, using loops will make it very slow and difficult. Could you please suggest me other methods that I can try? Please find a sample of data file and the procedure attached.
Thank you in advance.
  5 Comments
Hamid Basiri
Hamid Basiri on 8 Jun 2022 at 2:42
@Matt J could you please check the edited version of my question? Thank you in advance.

Sign in to comment.

Answers (2)

KSSV
KSSV on 17 May 2022
Edited: KSSV on 17 May 2022
data = importdata('dmp.txt') ;
[c,ia,ib]= unique(data(:,[1 end]),'rows','stable') ;
N = length(c) ;
iwant = cell(N,1) ;
for i = 1:N
d = data(ib==i,2:end-1) ;
iwant{i} = [data(ia(i),1) d(:)' data(ia(i),end)] ;
end
celldisp(i)

Matt J
Matt J on 17 May 2022
Edited: Matt J on 17 May 2022
T=varfun(@(x) [x(:)',nan(1,4-numel(x))],readtable('dmp.txt'),'Group',[9,1]);
T=T(:,[2,4:10,1])
T = 7×9 table
Var1 Fun_Var2 Fun_Var3 Fun_Var4 Fun_Var5 Fun_Var6 Fun_Var7 Fun_Var8 Var9 ____ ________________________________________ ________________________________________ ___________________________ ______________________________________________ ___________________________________________ ______________________________________ ____________________________________ ____ 13 -29.956 -30.023 -35.34 -35.518 -15.161 -15.12 -11.186 -11.012 159 152 -150 -157 0 -0.011841 -0.017602 -0.026036 0 0.006585 0.013008 0.026452 -1 -0.99991 -0.99976 -0.99931 202.88 199.29 193.49 188.41 1 13 15.044 15.045 15.124 15.124 -29.838 -29.839 -29.856 -29.856 159 152 -150 -157 0 8.28e-05 0.00026326 6.84e-05 0 -6.79e-05 -5.77e-05 3.52e-05 -1 -1 -1 -1 15131 15127 15122 15119 2 13 -8.4004 -8.4089 -8.8089 -8.8218 -26.56 -26.555 -26.495 -26.496 159 152 -150 -157 0 -0.0016628 -0.0013254 -0.0020045 0 0.00097067 0.00019455 -6.98e-05 -1 -1 -1 -1 1900.7 1896.7 1892.9 1887.8 3 -13 -21.318 -21.32 -21.036 -21.037 -1.9313 -1.9426 -2.7865 -2.8151 159 152 -150 -157 0 -0.00023584 0.00093533 -0.00046522 0 -0.0018652 -0.0027988 -0.004081 -1 -1 -1 -0.99999 1283.7 1279.9 1276.5 1272.8 4 13 -15.644 -15.6 -72.731 -75.632 22.889 22.751 23.197 23.309 159 152 -150 -157 0 0.006283 -0.36906 -0.38413 0 -0.022478 0.025045 0.011471 -1 -0.99973 -0.92907 -0.92321 324.09 320.83 114.04 109.06 5 13 23.595 23.617 22.209 22.206 -26.473 -26.472 -25.727 -25.689 159 152 -150 -157 0 0.0034162 -0.0046766 0.00011248 0 0.00020911 0.0024725 0.0065132 -1 -0.99999 -0.99999 -0.99998 475.73 472.24 468.49 464.96 6 13 -28.433 NaN NaN NaN 14.879 NaN NaN NaN 159 NaN NaN NaN 0 NaN NaN NaN 0 NaN NaN NaN -1 NaN NaN NaN 165.23 NaN NaN NaN 7

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by