Unique numbers, not ordered
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2021-6-4
@Dylan den Hartog: what should happen if:
- the maximum value occurs twice (or more)?
- the maximum does not occur in one (or more) of the vectors?
回答(1 个)
the cyclist
2021-6-4
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!