如何在不重复的情况下提取矩阵或数组的不同值/元素?

时间:2022-11-11 20:24:27

I have a vector/ or it could be array :

我有一个矢量/或它可能是数组:

A = [1,2,3,4,5,1,2,3,4,5,1,2,3]

I want to extract existing different values/elements from this vector without repeating:

我想从这个向量中提取现有的不同值/元素而不重复:

1,2,3,4,5
B= [1,2,3,4,5]

How can I extract it ?

我该如何提取它?

I would appreciate for any help please

如果您有任何帮助,我将不胜感激

2 个解决方案

#1


2  

Try this,

尝试这个,

A = [1,2,3,4,5,1,2,3,4,5,1,2,3]
y = unique(A)

B = unique(A) returns the same values as in a but with no repetitions. The resulting vector is sorted in ascending order. A can be a cell array of strings.

B =唯一(A)返回与a中相同的值但没有重复。生成的矢量按升序排序。 A可以是字符串的单元格数组。

B = unique(A,'stable') does the same as above, but without sorting.

B =唯一(A,'稳定')与上面相同,但没有排序。

B = unique(A,'rows') returns the unique rows ofA`.

B =唯一(A,'rows')返回唯一的A`行。

[B,i,j] = unique(...) also returns index vectors i and j such that B = A(i) and A = B(j) (or B = A(i,:) and A = B(j,:)).

[B,i,j] =唯一(...)也返回索引向量i和j,使得B = A(i)和A = B(j)(或B = A(i,:)和A = B (J,:))。


Reference: http://cens.ioc.ee/local/man/matlab/techdoc/ref/unique.html

参考:http://cens.ioc.ee/local/man/matlab/techdoc/ref/unique.html

Documentation: https://uk.mathworks.com/help/matlab/ref/unique.html

文档:https://uk.mathworks.com/help/matlab/ref/unique.html

#2


0  

The answers below are correct but if the user does not want to sort the data, you can use unique with the parameter stable

下面的答案是正确的,但如果用户不想对数据进行排序,则可以使用unique与参数stable

A = [1,2,3,4,5,1,2,3,4,5,1,2,3]
B = unique(A,'stable')

#1


2  

Try this,

尝试这个,

A = [1,2,3,4,5,1,2,3,4,5,1,2,3]
y = unique(A)

B = unique(A) returns the same values as in a but with no repetitions. The resulting vector is sorted in ascending order. A can be a cell array of strings.

B =唯一(A)返回与a中相同的值但没有重复。生成的矢量按升序排序。 A可以是字符串的单元格数组。

B = unique(A,'stable') does the same as above, but without sorting.

B =唯一(A,'稳定')与上面相同,但没有排序。

B = unique(A,'rows') returns the unique rows ofA`.

B =唯一(A,'rows')返回唯一的A`行。

[B,i,j] = unique(...) also returns index vectors i and j such that B = A(i) and A = B(j) (or B = A(i,:) and A = B(j,:)).

[B,i,j] =唯一(...)也返回索引向量i和j,使得B = A(i)和A = B(j)(或B = A(i,:)和A = B (J,:))。


Reference: http://cens.ioc.ee/local/man/matlab/techdoc/ref/unique.html

参考:http://cens.ioc.ee/local/man/matlab/techdoc/ref/unique.html

Documentation: https://uk.mathworks.com/help/matlab/ref/unique.html

文档:https://uk.mathworks.com/help/matlab/ref/unique.html

#2


0  

The answers below are correct but if the user does not want to sort the data, you can use unique with the parameter stable

下面的答案是正确的,但如果用户不想对数据进行排序,则可以使用unique与参数stable

A = [1,2,3,4,5,1,2,3,4,5,1,2,3]
B = unique(A,'stable')