필터 지우기
필터 지우기

How to fill arrays third column by matching first 2 columns

조회 수: 3 (최근 30일)
sastra university
sastra university 2022년 10월 19일
댓글: sastra university 2022년 10월 19일
Dear all,
I have 2 arrays namely A & B.
A is 30 x 3 array with 3rd column zero.
B is 15 x 3 array with the result value in the 3rd column.
I want to fill the 3rd column of A by matching the first 2 columns with B.
for example,
A=[1 5 0;
1 12 0;
4 5 0;
4 9 0;
7 8 0;
8 2 0;
9 13 0];
B = [1 5 2740;
1 12 10;
8 2 1500;
7 8 750];
I want to get the array A's 3rd column filled with the values by comparing B.
results like,
A = [1 5 2740;
1 12 10;
4 5 0;
4 9 0;
7 8 750;
8 2 1500;
9 13 0];
Please suggest me a code for doing this for the larger size of array.
Thank you in advance.

채택된 답변

KSSV
KSSV 2022년 10월 19일
A=[1 5 0;
1 12 0;
4 5 0;
4 9 0;
7 8 0;
8 2 0;
9 13 0];
B = [1 5 2740;
1 12 10;
8 2 1500;
7 8 750];
F = scatteredInterpolant(B(:,1),B(:,2),B(:,3)) ;
A(:,3) = F(A(:,1),A(:,2)) ;
A
A = 7×3
1.0e+03 * 0.0010 0.0050 2.7400 0.0010 0.0120 0.0100 0.0040 0.0050 1.9946 0.0040 0.0090 0.7700 0.0070 0.0080 0.7500 0.0080 0.0020 1.5000 0.0090 0.0130 -0.8471
  댓글 수: 3
KSSV
KSSV 2022년 10월 19일
A=[1 5 0;
1 12 0;
4 5 0;
4 9 0;
7 8 0;
8 2 0;
9 13 0];
B = [1 5 2740;
1 12 10;
8 2 1500;
7 8 750];
[c,ia] = ismember(B(:,1:2),A(:,1:2),'rows','legacy') ;
A(ia,3) = B(:,3)
A = 7×3
1 5 2740 1 12 10 4 5 0 4 9 0 7 8 750 8 2 1500 9 13 0
sastra university
sastra university 2022년 10월 19일
Thank you so much. Its working.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by