필터 지우기
필터 지우기

how to output a vector, when removing duplicates from a vector?

조회 수: 1 (최근 30일)
Sarah Sadeq
Sarah Sadeq 2016년 12월 13일
댓글: Sarah Sadeq 2016년 12월 13일
Hello, could someone help me with this question (it's just a practice problem for a test, not graded) I'd appreciate the help on this question, but instead of outputing the length, how could I output a vector of those numbers?
Question:
Given a sorted vector/array, write a function removeDuplicates in place such that each element in the array appears only onceand return the new length.
You cannot allocate extra space for another vector/array. The removal of elements should be done in place with constant memory.
For example, Given an input array vec = [4 5 5], your function should return 2.
my code:
function vec = removeDuplicates(n) h = zeros(1,numel(n)); for i = 1:numel(n)-1 for j = (i+1):numel(n) if n(j)==n(i) h(j)=1; end end end n(h==1)=[]; vec = numel(n); end

채택된 답변

David Barry
David Barry 2016년 12월 13일
편집: David Barry 2016년 12월 13일
The length:
numel(unique(vec))
Just the numbers:
unique(vec);
  댓글 수: 1
Sarah Sadeq
Sarah Sadeq 2016년 12월 13일
hm, I tried it but it didn't work. I'll try again. Thank you!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by