MATLAB Answers

Compare string array columns for equality then display all non-equal rows

조회 수: 2(최근 30일)
Vincent Scalfani
Vincent Scalfani 15 Jul 2018
답변: dpb 15 Jul 2018
Hello, I have 2 column string array data formatted like this:
x = ["1","2 2 0"; "2", "2 1 1"; "3", "1 1 0"; "4", "1 1 1"]
>>
"1" "2 2 0"
"2" "2 1 1"
"3" "1 1 0"
"4" "1 1 1"
y = ["1","x x x"; "2", "2 1 1"; "3", "1 1 0"; "4", "x x x"]
>>
"1" "x x x"
"2" "2 1 1"
"3" "1 1 0"
"4" "x x x"
I want to compare the second column for equality then display the entire rows that are different. So, first get the logical:
idx = x == y
>>
1 0
1 1
1 1
1 0
Now I am struggling to index back in (e.g. y(idx)) and display the non matching results like this for y:
"1" "x x x"
"4" "x x x"
and related:
for x
"1" "2 2 0"
"4" "1 1 1"
I'm only able to return a vertical 4 x 1 instead of 2 x 2. Thanks!

  댓글 수: 0

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

채택된 답변

dpb
dpb 15 Jul 2018
>> y(x(:,2)~=y(:,2),:)
ans =
2×2 string array
"1" "x x x"
"4" "x x x"
>>

  댓글 수: 0

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

추가 답변(0개)

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by