How can I align a sequence of three data points?
Show older comments
like I have a data sequence
data_A = [0 4 5 7 8 9], data_B = [4 5 7 8 1 5 6], data_C = [ 2 5 3 4 5 7 8]
I want result in this form..............Kindly help................
red square indicate matched sequence........

Accepted Answer
More Answers (2)
data_A = [0 4 5 7 8 9];
data_B = [4 5 7 8 1 5 6];
data_C = [2 5 3 4 5 7 8];
Note that this is not the best method to obtain the common sub-sequence, however, this works for this particular data set
y = intersect(intersect(data_A,data_B),data_C)
data = {data_A;data_B;data_C};
%number of elements in each vector
n = cellfun('length', data);
%the starting index of the sub-sequence in each vector
k = cellfun(@(x) strfind(x,y), data);
m = max(k);
nd = numel(data);
%preallocation
out = zeros(nd,max(n+m-k));
for idx = 1:nd
out(idx,m-k(idx)+(1:n(idx))) = data{idx};
end
out
2 Comments
CM Sahu
on 12 May 2023
Dyuman Joshi
on 12 May 2023
Yes, the trickiest part of this question is obtaining the common sub-sequence.
I will update my answer, if I am able to find a solution to that.
Hi Sahu,
Hope it helps.
% define the data sequences
data_A = [0 4 5 7 8 9];
data_B = [4 5 7 8 1 5 6];
data_C = [2 5 3 4 5 7 8];
% find the overlapping elements between data_A, data_B, and data_C
overlap_AB = intersect(data_A, data_B);
overlap_AC = intersect(data_A, data_C);
overlap_BC = intersect(data_B, data_C);
overlap_ABC = intersect(overlap_AB, overlap_AC);
overlap_ABC = intersect(overlap_ABC, overlap_BC);
% find the indices of the overlapping elements in data_A, data_B, and data_C
idx_overlap_A = ismember(data_A, overlap_ABC);
idx_overlap_B = ismember(data_B, overlap_ABC);
idx_overlap_C = ismember(data_C, overlap_ABC);
% plot the data sequences with red squares indicating the overlapping elements
plot(1:length(data_A), data_A, '-o', 'Color', 'b');
hold on;
plot(1:length(data_B), data_B, '-o', 'Color', 'g');
plot(1:length(data_C), data_C, '-o', 'Color', 'r');
scatter(find(idx_overlap_A), data_A(idx_overlap_A), 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r');
scatter(find(idx_overlap_B), data_B(idx_overlap_B), 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r');
scatter(find(idx_overlap_C), data_C(idx_overlap_C), 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r');
hold off;
Categories
Find more on System Composer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!