Hello Hasan,
Based on the requirements for combining seven matrices, there are two scenarios to consider depending on the uniformity of the data in the first column of each matrix. Here's how to approach both:
- When the first column of each matrix is the same
If the first column of each matrix is identical, indicating synchronized indices, you can directly combine the second columns of all matrices. Consider the following example matrices A1 to A7:
A1 =[-4:0.1:4 randi(100,1,81)];
A2 =[-4:0.1:4 randi(100,1,81)];
A3 =[-4:0.1:4 randi(100,1,81)];
A4 =[-4:0.1:4 randi(100,1,81)];
A5 =[-4:0.1:4 randi(100,1,81)];
A6 =[-4:0.1:4 randi(100,1,81)];
A7 =[-4:0.1:4 randi(100,1,81)];
result = [col2_A1 col2_A2 col2_A3 col2_A4 col2_A5 col2_A6 col2_A7];
- When only common indices should be merged
In cases where the first column (acting as an index) differs among matrices and matrices need to merge based on common indices, use the following function:
function result = mergeColumnsOnIndices(arrays)
commonIndices = arrays{1}(:, 1);
commonIndices = intersect(commonIndices, arrays{i}(:, 1));
result = zeros(length(commonIndices), length(arrays));
[~, idx] = intersect(arrays{i}(:, 1), commonIndices);
result(:, i) = arrays{i}(idx, 2);
Hope this helps.