I need help in arranging my data

3 views (last 30 days)
Please, find the attached matlab file The first column is the x_coordinates, second column is the y coordinates, the third column and fourth column are the data I want to plot. For my data to be correctly plotted, I need to rearrange my data such that x column will be from 0-2, 0-2, 0-2 ... and the corresponding y-coordinates and other data on the previous rows will go with x to the new rows.
For example
Initial matrix
0 0.4 0.6 0.8
1.5 0.3 0.5 0.7
1.8 0.5 0.8 0.4
1 0.7 0.9 0.5
1 0.5 0.4 0.3
2 0.2 0.1 0.6
0 0.8 0.9 0.4
1.5 0.2 0.1 0.6
2 0.2 0.4 0.1
0 0.3 0.4 0.6
1.5 0.2 0.1 0.9
1.8 0.4 0.5 0.7
1.8 0.1 0.2 0.4
1 0.2 0.4 0.5
2 0.3 0.2 0.3
What I want to acheive
0 0.4 0.6 0.8
1 0.7 0.9 0.5
1.5 0.3 0.5 0.7
1.8 0.5 0.8 0.4
2 0.2 0.1 0.6
0 0.8 0.9 0.4
1 0.5 0.4 0.3
1.5 0.2 0.1 0.6
1.8 0.4 0.5 0.7
2 0.2 0.4 0.1
0 0.3 0.4 0.6
1 0.2 0.4 0.5
1.5 0.2 0.1 0.9
1.8 0.1 0.2 0.4
2 0.3 0.2 0.3
Please, I need a code that can help me to achieve this. My original data is the phi.mat attached. Thank you so much.
  2 Comments
Stephen23
Stephen23 on 17 Aug 2018
@SAMUEL AYINDE: why does the first line disappear from your data? You show it in the input matrix, but not in the output matrix.
SAMUEL AYINDE
SAMUEL AYINDE on 17 Aug 2018
@Stephen Cobeldick, It was a mistake. The first line should not be there in the first place. Thank you so much.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 17 Aug 2018
Edited: Stephen23 on 17 Aug 2018
The simplest solution is to use sortrows. Your real data (attached) has some floating point errors which mean that values that look the same are not actually the same at all. I dealt with that using round:
>> [~,idx] = sortrows(round(phi(:,1:2)*1e4),[2,1]);
>> out = phi(idx,:);
>> out(1:100,:) % the first one hundred rows
ans =
0.00000 0.00000 0.12500 4.35415
0.02500 0.00000 0.12625 3.87539
0.05000 0.00000 0.12995 -6.60861
0.07500 0.00000 0.13333 -32.63957
0.10000 0.00000 0.10833 -15.64291
0.12500 0.00000 0.08333 -3.38352
0.15000 0.00000 0.05833 0.99885
0.17500 0.00000 0.03333 6.29332
0.20000 0.00000 0.00833 7.43663
0.22500 0.00000 -0.01667 9.02729
0.25000 0.00000 -0.04167 11.19658
0.27500 0.00000 -0.06667 13.11854
0.30000 0.00000 -0.09167 20.88033
0.32500 0.00000 -0.11667 66.35252
0.35000 0.00000 -0.10833 39.24750
0.37500 0.00000 -0.08333 16.96182
0.40000 0.00000 -0.05833 12.69074
0.42500 0.00000 -0.03333 10.41462
0.45000 0.00000 -0.00833 8.43611
0.47500 0.00000 0.01667 7.01445
0.50000 0.00000 0.04167 5.98383
0.52500 0.00000 0.06667 -2.22252
0.55000 0.00000 0.09167 -6.89060
0.57500 0.00000 0.11667 -23.43924
0.60000 0.00000 0.13374 -33.44865
0.62500 0.00000 0.12845 0.60804
0.65000 0.00000 0.12555 4.44767
0.67500 0.00000 0.12514 4.45818
0.70000 0.00000 0.12721 3.00757
0.72500 0.00000 0.13172 -17.72344
0.75000 0.00000 0.12500 -29.57837
0.77500 0.00000 0.10000 -9.43113
0.80000 0.00000 0.07500 -1.13979
0.82500 0.00000 0.05000 4.74838
0.85000 0.00000 0.02500 6.63524
0.87500 0.00000 0.00000 7.90819
0.90000 0.00000 -0.02500 9.68690
0.92500 0.00000 -0.05000 11.98908
0.95000 0.00000 -0.07500 13.08059
0.97500 0.00000 -0.10000 25.11572
1.00000 0.00000 -0.12500 80.30814
1.02500 0.00000 -0.10000 25.11572
1.05000 0.00000 -0.07500 13.08059
1.07500 0.00000 -0.05000 11.98908
1.10000 0.00000 -0.02500 9.68690
1.12500 0.00000 -0.00000 7.90819
1.15000 0.00000 0.02500 6.63524
1.17500 0.00000 0.05000 4.74838
1.20000 0.00000 0.07500 -1.13979
1.22500 0.00000 0.10000 -9.43113
1.25000 0.00000 0.12500 -29.57837
1.27500 0.00000 0.13172 -17.72344
1.30000 0.00000 0.12721 3.00757
1.32500 0.00000 0.12514 4.45818
1.35000 0.00000 0.12555 4.44767
1.37500 0.00000 0.12845 0.60804
1.40000 0.00000 0.13374 -33.44865
1.42500 0.00000 0.11667 -23.43924
1.45000 0.00000 0.09167 -6.89060
1.47500 0.00000 0.06667 -2.22252
1.50000 0.00000 0.04167 5.98383
1.52500 0.00000 0.01667 7.01445
1.55000 0.00000 -0.00833 8.43611
1.57500 0.00000 -0.03333 10.41462
1.60000 0.00000 -0.05833 12.69074
1.62500 0.00000 -0.08333 16.96182
1.65000 0.00000 -0.10833 39.24750
1.67500 0.00000 -0.11667 66.35252
1.70000 0.00000 -0.09167 20.88033
1.72500 0.00000 -0.06667 13.11854
1.75000 0.00000 -0.04167 11.19658
1.77500 0.00000 -0.01667 9.02729
1.80000 0.00000 0.00833 7.43663
1.82500 0.00000 0.03333 6.29332
1.85000 0.00000 0.05833 0.99885
1.87500 0.00000 0.08333 -3.38352
1.90000 0.00000 0.10833 -15.64291
1.92500 0.00000 0.13333 -32.63957
1.95000 0.00000 0.12995 -6.60861
1.97500 0.00000 0.12625 3.87539
2.00000 0.00000 0.12500 4.35415
0.00000 0.02500 0.10000 4.64482
0.02500 0.02500 0.10138 4.45428
0.05000 0.02500 0.10549 3.43575
0.07500 0.02500 0.11217 -27.11467
0.10000 0.02500 0.10967 -48.24988
0.12500 0.02500 0.08483 -3.38557
0.15000 0.02500 0.06003 5.46925
0.17500 0.02500 0.03529 6.33332
0.20000 0.02500 0.01066 7.51564
0.22500 0.02500 -0.01382 9.21827
0.25000 0.02500 -0.03800 11.81698
0.27500 0.02500 -0.06154 15.89211
0.30000 0.02500 -0.08333 21.18223
0.32500 0.02500 -0.09865 46.15575
0.35000 0.02500 -0.09495 32.23685
0.37500 0.02500 -0.07641 19.42267
0.40000 0.02500 -0.05380 14.33840
0.42500 0.02500 -0.02999 10.81766
0.45000 0.02500 -0.00568 8.57450

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!