필터 지우기
필터 지우기

Remove repeated values in a CSV string

조회 수: 4 (최근 30일)
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019년 4월 19일
댓글: Matthew Tyler Jeffries 2019년 4월 22일
I have a string with comma separated values, and would like to delete the repeated values so that there are no repeats in my string.
mystring = '1,1,1,25,33,33,5,5,3,2,1,1'
Iwant = '1,25,33,5,3,2'
The order of the numbers in "Iwant" is not important. It could be '1,25,33,5,3,2' or "1,33,5,25,3,2" or "2,3,5,33,1,25" (whatever works).
I have many other strings I would like to edit, so it would be great if the code could apply to a string with any manner of numbers in it.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 4월 19일
Are the values certain to be numeric? Are they certain to be integer? If they might be floating point, then would 25.70 be considered different than 25.7 ?
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019년 4월 19일
Yes, the values will always be numeric, no decimals, no fractions, no negative numbers.

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

채택된 답변

Stephan
Stephan 2019년 4월 19일
편집: Stephan 2019년 4월 19일
mystring = '1,1,1,25,33,33,5,5,3,2,1,1'
iwant = join(unique(split(string(mystring),',')),',')
results in a string:
mystring =
'1,1,1,25,33,33,5,5,3,2,1,1'
iwant =
"1,2,25,3,33,5"
Note, that you speak of string, but use char type. if you want char as result use this instead:
iwant = char(join(unique(split(string(mystring),',')),','))
  댓글 수: 1
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019년 4월 22일
Thank you! This is exactly what I need.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by