List all values in an array without repeats

让人想犯罪 __ 提交于 2019-12-20 05:56:52

问题


Suppose I have a 100 x 100 matrix composed of some combination of 250s, 125s, 15s and 9s. I would like to return a sorted vector of all of the unique values in this matrix.

Something in the matter of:

sort(somefunction(matrix))=vector 

The result I would like to get is thus:

vector=9,15,125,250

Is there a quick and easy way to do this?


回答1:


b = unique(a)

Check the docs on unique

A = randi(9,10,10);
unique(A)
ans =
     1
     2
     3
     4
     5
     6
     7
     8
     9



回答2:


b = sort(a(:));

This should do the work sort your matrix;

And this shall return all values into a vector.

b = unique(a(:));


来源:https://stackoverflow.com/questions/34641200/list-all-values-in-an-array-without-repeats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!