MATLAB: Create new variable that recognizes changes in the code

cell arraydoublematch

I have a double matrix B that has in C1 the year 1998 and in C2 a code (unrepeated). Each C2 is given a value in C3.
% C1 C2 C3
B=[ 1998 22 37
1998 34 7
1998 76 12
1998 98 29
1998 107 14
]
This is how I got to B:
N1=[N{:,1} N{:,2} N{:,3}];
N1=unique(N1(:,1:3),'rows');
N3= unique(N1(:,1:2),'rows');
for m=1:size(N3,1)
N3(m,3)=sum(N1(:,1) == N3(m,1) & N1(:,2)==N3(m,2));
end
B=N3((N3(:,1) == 1998),:);
I have a cell-array A with the years horizontally disposed in R1, un-repeated values in Y, and corresponding codes in the columns that follow. The codes are the same as in C2 from variable B, but disposed differently.
A={Y 1996 1997 1998 1999 %R1
1 107 107 22 22
13 98 98 76 1267
}
Is there a way I could get a new variable that recognizes the change in the codes in variable A, and presents the corresponding values from C3 in B? For instance:
AB={Y Initial C2 Change C2
1 107 14 22 37
13 98 29 76 12 }

Best Answer

Given the A and B from above (less the incomplete portions), you could iterate over each row of A and compare the second and fourth values with that from distinct second column of B, looking for a match. If a match is found, then replace the third and fifth values form AB with those from B (if a match for both can be found)
% initialize AB to that of A
AB = A;
% iterate over each row of A
for k=1:size(A,1)
% search the second column of B for the second element of A
idx = find(B(:,2)==A{k,2});
if ~isempty(idx)
% replace

AB{k,3} = B(idx,3);
else
% not found, so set to empty (?)

AB{k,3} = [];
end
% search the second column of B for the fourth element of A
idx = find(B(:,2)==A{k,4});
if ~isempty(idx)
% replace
AB{k,5} = B(idx,3);
else
% not found, so set to empty (?)
AB{k,5} = [];
end
end
Try the above and see what happens!