MATLAB: Replacing values for matrices of different dimension

array

Suppose,
A= [15 1 4 2 65
15 2 4 6 65
17 6 5 2 65
24 5 3 1 55
24 5 5 3 55
25 2 1 1 55
30 2 1 1 20
31 5 2 2 11
31 5 3 5 11
33 3 2 2 31
33 4 5 3 31
57 3 2 4 2
58 4 5 5 2];
B= [1 1 1
1 1 5
1 1 6
1 2 3
1 3 2
2 1 4
2 1 6
2 2 2
2 2 5
2 3 2
3 1 6
3 2 1
3 2 6
3 3 3
3 3 4
3 3 5
3 4 2
3 4 3
3 4 4
3 4 5
4 1 1
4 2 1
4 2 3
4 2 4
4 2 5
4 3 6
4 6 6
5 1 1
5 1 2
5 1 3
5 1 4
5 1 5
5 4 3
5 5 1
5 6 6
6 1 2
6 1 4
6 1 6
6 2 1
6 2 2
6 2 3];
Both A and B are matrices of different dimensions. I would like to substitute the values in A(:,2:4) with values from B(:,1:3) based on A(column 2).
Resultant (Something like this):
Resultant=[ 15 1 1 1 65
15 2 1 4 65
17 6 1 2 65
24 5 1 1 55
24 5 5 3 55
25 5 1 2 55
30 2 1 6 20
31 5 1 3 11
31 5 1 4 11
33 3 1 6 31
33 4 1 1 31
57 3 2 1 2
58 4 2 1 2];

Best Answer

[a,ia0] = sort(A(:,2));
ba = histc(a,unique(a));
t1 = [true;diff(B(:,1))~=0] + 0;
t1(find(t1) + ba)= -1;
t3 = cumsum(t1);
[ii,ii] = sort(ia0);
b = B(t3>0,:);
out = A;
out(:,2:4) = b(ii,:);
or
[ba,i00] = histc(A(:,2),unique(A(:,2)));
out = A;
for jj = 1:numel(ba)
t = find(B(:,1) == jj);
out(i00 == jj,2:4) = B(t(1:ba(jj)),:);
end