To achieve this, you want to create a 10×2 matrix C where:
- The first column contains all elements from vector A.
- The second column contains the elements from vector B, but only those that have corresponding matches in A and in the same position.
You can accomplish this by iterating over the elements of vector A and finding the corresponding matches in vector B. Here's how you can do it in MATLAB:
% Define the input vectors
A = [1; 3; 7; 10; 12; 14; 15; 18; 20; 21];
B = [3; 12; 15; 18; 20; 0; 0; 0; 0; 0];
% Initialize the output matrix C
C = zeros(length(A), 2);
% Fill the first column of C with elements from A
C(:, 1) = A;
% Loop through each element in A to find matches in B
for i = 1:length(A)
% Find the index of A(i) in B
index = find(B == A(i));
% If a match is found, place it in the second column of C
if ~isempty(index)
C(i, 2) = B(index(1));
else
% If no match is found, ensure the second column is zero
C(i, 2) = 0;
end
end
% Display the result
disp(C);