how to add columns to a matrix that matches a row value

조회 수: 1 (최근 30일)
Philippe Corner
Philippe Corner 2020년 2월 27일
답변: Fangjun Jiang 2020년 2월 27일
If we have a matrix M and a matrix N, how could we add the columns 2 and 3 of N matrix (N(:,2:3)) to the rows that contain the same value of matrix M...
M=[ -4.000 1559.261 37.879 0.0264 0.304
4.000 1557.432 31.819 0.0314 28.378
12.000 1555.531 53.517 0.0187 173.220
20.000 1554.017 47.451 0.0211 59.931
60.000 1546.429 116.360 0.008594 216.040
68.000 1545.598 42.564 0.0235 100.640
76.000 1544.164 16.102 0.0621 6.075
84.000 1542.348 8.781 0.1139 12.563
-4.000 1556.801 890.960 0.001122 0.100
4.000 1554.965 333.730 0.002996 3.239
12.000 1553.069 94.732 0.0106 1.705
20.000 1551.491 65.060 0.0154 42.051
28.000 1550.359 95.353 0.0105 27.934
36.000 1547.935 21.962 0.0455 98.265
44.000 1545.010 41.541 0.0241 10.258
52.000 1543.880 81.797 0.0122 0.992
60.000 1543.565 38.686 0.0258 0.799
68.000 1542.451 42.857 0.0233 0.938];
N=[ -4 434079.859000000 685367.638500000
4 434075.109000000 685361.682500000
12 434071.550500000 685354.932500000
20 434068.490500000 685347.831500000
28 434064.844500000 685341.188500000
36 434062.067000000 685335.580500000
44 434059.115000000 685329.999500000
52 434056.541000000 685323.122000000
60 434052.816000000 685316.572500000
68 434047.564000000 685310.242500000
76 434043.778000000 685303.228500000
84 434039.789000000 685296.990000000];
.. and obtain matrix R like this (made manually):
R=[
-4 434079.859 685367.6385 1559.261 37.879 0.0264 0.304
4 434075.109 685361.6825 1557.432 31.819 0.0314 28.378
12 434071.5505 685354.9325 1555.531 53.517 0.0187 173.22
20 434068.4905 685347.8315 1554.017 47.451 0.0211 59.931
60 434052.816 685316.5725 1546.429 116.36 0.008594 216.04
68 434047.564 685310.2425 1545.598 42.564 0.0235 100.64
76 434043.778 685303.2285 1544.164 16.102 0.0621 6.075
84 434039.789 685296.99 1542.348 8.781 0.1139 12.563
-4 434079.859 685367.6385 1556.801 890.96 0.001122 0.1
4 434079.859 685361.6825 1554.965 333.73 0.002996 3.239
12 434075.109 685354.9325 1553.069 94.732 0.0106 1.705
20 434071.5505 685347.8315 1551.491 65.06 0.0154 42.051
28 434068.4905 685341.1885 1550.359 95.353 0.0105 27.934
36 434062.067 685335.5805 1547.935 21.962 0.0455 98.265
44 434059.115 685329.9995 1545.01 41.541 0.0241 10.258
52 434056.541 685323.122 1543.88 81.797 0.0122 0.992
60 434052.816 685316.5725 1543.565 38.686 0.0258 0.799
68 434047.564 685310.2425 1542.451 42.857 0.0233 0.938];

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 2월 27일
[~,index]=ismember(M(:,1),N(:,1));
R=[M(:,1),N(index,2:3),M(:,2:end)]

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by