How to find unique elements in a vector without using unique or find?

조회 수: 9 (최근 30일)
I have A=randi(100,1,100); How can I find unique values in array and show them without using unique() and find() .
  댓글 수: 5
John D'Errico
John D'Errico 2016년 10월 24일
Yes. It CAN be done using basic MATLAB statements.
For example, using sort, then diff and find will make it absolutely trivial. And if you cannot use unique, then sort, diff and find are off limits too.
But this is homework. You need to do your homework. There is simply no reason for this question under any other circumstances. It was assigned to you, for you to learn. If we doit for you then you learn nothing but how to get others to do your work.
Rena Berman
Rena Berman 2017년 1월 20일
편집: Image Analyst 2017년 12월 11일
(Answers Dev) Restored Question.
I have
A=randi(100,1,100);
How can I find unique values in array and show them without using unique() and find() .

댓글을 달려면 로그인하십시오.

채택된 답변

Massimo Zanetti
Massimo Zanetti 2016년 10월 23일
편집: Massimo Zanetti 2016년 10월 23일
Use an histogram based approach (works only for positive integers)
%get M random numbers from 1 to N
M=10; N=25;
A=randi(N,1,M)
%find unique values
x=1:N;
unique_vals = x(logical(accumarray(A',1,[N,1])))
  댓글 수: 1
Trevor Badji
Trevor Badji 2016년 10월 24일
This works thanks however I need to solve this problem without using any matlab functions . Can we do this with using for or if ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Thorsten
Thorsten 2016년 10월 24일
편집: Thorsten 2016년 10월 24일
You can do this with for and if:
suppose you have the numbers in x
and want to generate the unique numbers in xu
pick the first number of x, this is your first unique number in xu
now run through all the number in x
check if it is in xu
if so, proceed with the next number of x
else, add the number to your unique numbers x
Voilá
This is a possible algorithm, and it is up to you to implement it, to learn something.
  댓글 수: 1
Trevor Badji
Trevor Badji 2016년 10월 24일
I got the algorithm thanks for the reply . But Im new at matlab I do not know how to add the number to xu for example . I can write this code by C++ however I am having trouble in matlab . I get errors mostly

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by