contains 函数在运行相同代码时​,为什么返回了不同的​逻辑数组?

조회 수: 2 (최근 30일)
笑天
笑天 2024년 5월 22일
편집: Voss 2024년 5월 22일
a=[ "水果店" "武汉水果店" "宜昌水果店" "襄阳水果店" "荆州水果店"
"饭店" "武汉饭店" "宜昌饭店" "襄阳饭店" "荆州饭店" ];
store_name ={'宜昌水果店','武汉水果店'};
T=contains(a,store_name{1});
T
T = 2x5 logical array
0 0 0 0 0 0 0 0 0 0
store_name ={'宜昌水果店','武汉水果店'};
T=contains(a,store_name{1});
T
T = 2x5 logical array
0 0 1 0 0 0 0 0 0 0

채택된 답변

Voss
Voss 2024년 5월 22일
편집: Voss 2024년 5월 22일
There is a hidden character at the beginning of the first store_name{1}
store_name ={'宜昌水果店','武汉水果店'};
+store_name{1} % character codes
ans = 1x6
65279 23452 26124 27700 26524 24215
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
which is not in the second store_name{1}
store_name ={'宜昌水果店','武汉水果店'};
+store_name{1} % character codes
ans = 1x5
23452 26124 27700 26524 24215
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
There are various ways to remove that extra character, e.g., using strrep:
store_name ={'宜昌水果店','武汉水果店'}; % 1st one again
+store_name{1} % character codes
ans = 1x6
65279 23452 26124 27700 26524 24215
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
store_name{1} = strrep(store_name{1},char(65279),'');
+store_name{1} % character codes
ans = 1x5
23452 26124 27700 26524 24215
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now they are the same and produce the same result when checking against a.
a=[ "水果店" "武汉水果店" "宜昌水果店" "襄阳水果店" "荆州水果店"
"饭店" "武汉饭店" "宜昌饭店" "襄阳饭店" "荆州饭店" ];
store_name ={'宜昌水果店','武汉水果店'};
store_name{1} = strrep(store_name{1},char(65279),'');
T = contains(a,store_name{1})
T = 2x5 logical array
0 0 1 0 0 0 0 0 0 0
store_name ={'宜昌水果店','武汉水果店'};
T = contains(a,store_name{1})
T = 2x5 logical array
0 0 1 0 0 0 0 0 0 0

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!