How can I find matching rows between two matrices and and retrieve a value to be applied to the matched row?

I'm trying to take matrix (A) & search matrix (B) for matching rows (matching columns 1-5) and retrieve the value from (B) column 6 to be added to matrix (A) column 6.
A={1 1 1 1 1 2; 1 1 1 1 2 3; 1 1 1 1 3 1; 1 1 1 1 4 2; 1 1 1 1 5 8;
1 1 1 1 6 3; 1 1 1 1 7 1; 1 1 1 1 8 0; 1 1 1 1 9 9; 1 1 1 2 0 2};
B={1 1 1 1 2 1; 1 1 1 1 4 1; 1 1 1 1 6 2; 1 1 1 2 0 3; 1 1 1 1 9 3;
1 1 1 1 1 0; 1 1 1 1 3 0; 1 1 1 1 5 0; 1 1 1 1 7 4; 1 1 1 1 8 8};
% Find A's match within B's matrix
[~,~,ib] = intersect(A(:, 1:5), B(:, 1:5), 'rows');
% Get Adjustment (Column 6) values from B
adjustment = B(ib, 6);
% Apply Adjustments to B
for z = 1:length(RxPDW)
A(z,6) = sum(A(z,6),adjustment(z,1));
end
I'm trying get a result of
A={1 1 1 1 1 2; 1 1 1 1 2 4; 1 1 1 1 3 1; 1 1 1 1 4 3; 1 1 1 1 5 9; 1 1 1 1 6 5; 1 1 1 1 7 9; 1 1 1 1 8 13; 1 1 1 1 9 12; 1 1 1 2 0 9}

 採用された回答

dpb
dpb 2015 年 11 月 12 日
I don't think your desired result is consistent with your input...
>> [A(:,6) B(ib,6)]
ans =
2 0
3 1
1 0
2 1
8 0
3 2
1 4
0 8
9 3
2 3
>>
I get instead
>> A(ia,6)=A(ia,6)+B(ib,6)
A =
1 1 1 1 1 2
1 1 1 1 2 4
1 1 1 1 3 1
1 1 1 1 4 3
1 1 1 1 5 8
1 1 1 1 6 5
1 1 1 1 7 5
1 1 1 1 8 8
1 1 1 1 9 12
1 1 1 2 0 5
>>
which seems consistent if I look at the results from intersect which also seems consistent with the input by eye...

1 件のコメント

Patrick Conaway
Patrick Conaway 2015 年 11 月 12 日
編集済み: Patrick Conaway 2015 年 11 月 13 日
Thanks dpb! fairly new to Matlab, that worked perfectly & is cleaner than my for loop (now to try it on my 1.5million line data sample)
& output was slightly off, i was hand jamming an example while wife was prepping dinner...
THANKS AGAIN!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2015 年 11 月 12 日

編集済み:

2015 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by