Unique numbers, not ordered

8 views (last 30 days)
Dylan den Hartog
Dylan den Hartog on 4 Jun 2021
I have three matrices:
X1 = [1 23 36 45 47 2]'
X2 = [1 36 47 2 6]'
X3 = [1 22 23 47 2 4]'
I want to end up with a matrix like this:
X = [1 22 23 36 45 47 2 4 6]
I tried using this code:
unique([X1;X2;X3],'stable')
And this came out:
X = [1 23 36 45 47 2 6 22 4]
The numbers go up to 47 and then they go from low to high again. When the numbers go up again they are all unique numbers, so numbers that didn't occur the first time when the numbers went up.
X1 = [1 23 36 45 47 ** 2]'
X2 = [1 36 47 ** 2 6]'
X3 = [1 22 23 47 ** 2 4]'
I want to get the unique numbers bofore the drop, indicated with **, and after the drop.
X_before = [1 22 23 36 45 47]'
X_after = [2 4 6]
And then I can get the matrix I want:
X = [X_before; X_after]
= [1 22 23 36 45 47 2 4 6]
  4 Comments
Stephen23
Stephen23 on 4 Jun 2021
@Dylan den Hartog: what should happen if:
  1. the maximum value occurs twice (or more)?
  2. the maximum does not occur in one (or more) of the vectors?
Dylan den Hartog
Dylan den Hartog on 4 Jun 2021
Edited: Dylan den Hartog on 4 Jun 2021
  1. The maximum value does not occur twice or more. When the numbers drop and go up again these will all be new (unique) numbers.
  2. When the maximum does not occur in one (or more) of the other vectors the maximum of that vector should be taken as a drop point.
So lets say we now have this:
X1 = [1 23 36 45 47 2]'
X2 = [1 36 47 2 6]'
X3 = [1 22 23 46 2 4]'
In X3 the first maximum is now 46 instead of 47. In the end I want to end up with this vector:
X = [1 22 23 36 45 46 47 2 4 6]

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 4 Jun 2021
I believe this does what you want:
X1 = [1 23 36 45 47 2]';
X2 = [1 36 47 2 6 ]';
X3 = [1 22 23 47 2 4]';
peak1 = find(diff(X1)<0,1);
peak2 = find(diff(X2)<0,1);
peak3 = find(diff(X3)<0,1);
X_u = unique([X1(1:peak1); X2(1:peak2); X3(1:peak3)])
X_d = unique([X1(peak1+1:end); X2(peak2+1:end); X3(peak3+1:end)])
X_sort = [X_u; X_d]
This will break if the last value of any of the original vectors is the max, but that is easily fixed up.
  2 Comments
Dylan den Hartog
Dylan den Hartog on 4 Jun 2021
Edited: Dylan den Hartog on 4 Jun 2021
This is almost what I want! The issue is that there is not just one drop (or peak) per matrix but there are multiple. So lets say the matrices are:
X1 = [1 23 36 45 47 2 20 25 3 7 10 5]';
X2 = [1 36 47 2 6 24 3 7 11 5]';
X3 = [1 22 23 47 2 4 25 3 7 11 5] ';
I should end up like this
X = [1 22 23 36 45 47 2 4 6 20 24 25 3 7 10 11 5]'
Dylan den Hartog
Dylan den Hartog on 4 Jun 2021
I think I know how to do it now. Thanks
X1 = [1 23 36 45 47 2 20 25 3 7 10 5]';
X2 = [1 36 47 2 6 24 3 7 11 5]';
X3 = [1 22 23 47 2 4 25 3 7 11 5]';
[~,peaks1] = findpeaks(X1);
[~,peaks2] = findpeaks(X2);
[~,peaks3] = findpeaks(X3);
for i = 1
X_u{i,1} = unique([X1(1:peaks1(i)); X2(1:peaks2(i)); X3(1:peaks1(i))]);
end
for i = 2:length(peaks1)
X_u{i,1} = unique([X1(peaks1(i-1)+1:peaks1(i)); X2(peaks2(i-1)+1:peaks2(i)); X3(peaks3(i-1)+1:peaks3(i))]);
end
for i = length(peaks1)+1
X_u{i,1} = unique([X1(peaks1(i-1)+1:end); X2(peaks2(i-1)+1:end); X3(peaks3(i-1)+1:end)]);
end
X_sort = vertcat(X_u{:})

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!