How to compare two arrays of strings?

조회 수: 18 (최근 30일)
Luis Angel Manriquez Ramirez
Luis Angel Manriquez Ramirez 2022년 11월 15일
댓글: Stephen23 2022년 11월 28일
Hi everyone! Currently I'm creating an with Appdesigner and I would like to compare some arrays of strings as follows:
Let's say I have two arrays:
A=['red','purple','blue','green'];
B=['orange','purple','yellow','green'];
I want to compare each element in array 'B' with array 'A'. If the element in B is diferent from A I want to save that element in a new array C and use a counter to count every element that is diferent from A.
This is what I would get after the comparison:
C=['orange','yellow']
counter=2
I hope someone can help. thanks for reading!
  댓글 수: 1
Stephen23
Stephen23 2022년 11월 28일
A=["red","purple","blue","green"];
B=["orange","purple","yellow","green"];
C = setdiff(B,A)
C = 1×2 string array
"orange" "yellow"

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

채택된 답변

David Hill
David Hill 2022년 11월 15일
Use string arrays instead of character arrays.
A=["red","purple","blue","green"];
B=["orange","purple","yellow","green"];
C=B(~ismember(B,A))
C = 1×2 string array
"orange" "yellow"
Look at what a charachter array looks like
A=['red','purple','blue','green']%it is all bunched together, the commas are meaningless
A = 'redpurplebluegreen'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by