MATLAB: Element extraction from Matrix based on column index from another matrix.

column and row indexingmatrix manipulation

Good afternoon/good day Matlab community, I would like some help with an exercise that I am trying to complete giving two input matrices, A and B. Suppose A is(3*3) and B is (3*9). The first two columns in both matrices have unique ID's to each row A(:,3) and B(:,9) The output matrix C is C(3*3) with C(:,1) and C(:,2) are exactly the same as A(:,1) and A(:,2), and C(:,3) should be extracted from matrix B using A(:,1) unique identifier to match B(:,1) unique IDs while indexing the column position using A(:,2)+1. Please see the m.code with the example attached, (to make it clear, A is a matrix not a cell array, but I had to build it into a cell array to differentiate the column names where the first column has unique identifier info for each row) Thank you so much.
%%A is the first matrix, first column has unique identifier for each row....
%%%B is the second input matrix (3*9) and the first column has unique
%%%identifiers for each row.
%%%C is the desired output matrix: with the first column containing the
%%%same unique identifiers as matrix A, second column has the same info
%%%from Matrix A as well, and the 3rd column elements are basically
%%%extracted from B based on column indexing using A(:,2).
%%%Example here
A={'UniqueID', 19 17 15 ;'ColumnIndex', 3 5 7 ;'Carbonvalue', 5 3 9}'
B=[ 15 2 3 5 8 9 7 4 5 ;19 2 2.5 3 3.5 4 4.5 5 5.5 ; 17 1 5 4 3 6 8 7 4 ];
C=[19,3, 3; 17,5,6; 15,7,4];
%%so for example C(1,1)=A(1,1)
%%%C(1,2)=A(1,2)
%%%%C(1,3)=The element from B with the same row unique
%%%%identifier as A, but at column index A(1,2)+1
%%%The reason I indexed it using +1 because the elements of first column
%%%of B all unique idnetifiers, so B data starts from column
%%%2 through column 9.
%%%%%%%

Best Answer

The fact that A is a cell array makes things a bit awkward, but otherwise it's trivial to do with ismember to find the matching rows, and sub2ind to index B from the row and column indices:
Avalues = cell2mat(A(2:end, :)); %to make things easier
[isinB, Brow] = ismember(Avalues(:, 1), B(:, 1));
assert(all(isinB), 'Some ID in A are not in B');
C = [Avalues(:, [1 2]), B(sub2ind(size(B), Brow, Avalues(:, 2) + 1))]