How would I create my own 'unique' function without using the built in unique?

I understand that the built in Unique function is stating that whatever function you define as unique is the same data set but without repetitions.
should I create a function file such as
function[unique,c] = myuni_1(A)
rows(A)= size(A,1);
columns(A) = size(A,2);
and then proceed to define the rows and columns as itself? I'm a bit confused here.

댓글 수: 3

Why would you want to do this? Is this a homework assignment?
My TA mentioned my professor may ask a question like this on my final exam and I would like to be prepared for anything. Also, my own curiosity is driving me to ask it.
I've looked at the function file on Matlab and it is a very long code. I'm curious if this is even possible with an alternative method that uses less code.
Jan
Jan 2014년 4월 30일
편집: Jan 2014년 4월 30일
What exactly should your function do? Which types of input are expected? What is the 2nd output c? Please do not let us guess the details.
Notice that "rows(A)= size(A,1);" is no valid Matlab syntax.

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

 채택된 답변

Jan
Jan 2014년 4월 30일
A short version:
function U = myUnique(A)
[As, SV] = sort(A(:));
UV(SV) = ([1; diff(As)] ~= 0); % [1, As(2:nA) - As(1:nA-1)];
U = A(UV);

댓글 수: 1

works like a charm, i was using 3 different for loops and trying to sort each one by the size of A but got nothing.
Thank you friend!

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2014년 4월 29일
Suppose you have two "bags", A and B. Start with A empty.
If B is empty, you are done and A holds the unique values. Otherwise, take one one item, C, out of B (removing it from B). Is there already a copy of C in A? If there is, then throw away C. Otherwise, add C to A. Now restart this paragraph.

댓글 수: 1

im just confused about how to pull out a value and place it into the new matrix

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

Sean de Wolski
Sean de Wolski 2014년 4월 30일
편집: Sean de Wolski 2014년 4월 30일
How about this?
x = [1 2 3 3 3 3 2 4 5];
[uv,ia] = intersect(x,x)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

태그

질문:

2014년 4월 29일

편집:

2014년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by