フィルターのクリア

Comparing to string vectors

2 ビュー (過去 30 日間)
Inês Mendes
Inês Mendes 2015 年 7 月 8 日
コメント済み: James Tursa 2015 年 7 月 8 日
Hi guys,
I am having a lot of trouble comparing 2 arrays of strings.
For example, I have:
date = ['12-10-1992';'13-10-1992';'14-10-1992';'15-10-1992'];
date2=['13-10-1992';'14-10-1992'];
I want to compare them and find the position for which date 2 is equal to date.
I tried using find, ismember, intersect..none of them are working and i don´t understand why..
can anyone help please?
I would be much appreciated!
Inês
  2 件のコメント
James Tursa
James Tursa 2015 年 7 月 8 日
Please verify first what class of variables you have. What is class(date) and class(date2)?
Inês Mendes
Inês Mendes 2015 年 7 月 8 日
they are both cell type..

サインインしてコメントする。

採用された回答

bmeyer
bmeyer 2015 年 7 月 8 日
hey
The way you are defining your arrays causes them to be stored as type int. For example:
date = [12-10-1992;13-10-1992]; -----> date = [-1990,-1989]
date2 = [13-10-1992;14-10-1992]; -----> date2 = [-1989,-1988]
I suppose in this case, you would get lucky because comparing same "dates" would be comparing the integer values, which would turn out to be the same. However, if you actually want to comparing strings, you should define them like this:
date = ['12-10-1992','13-10-1992'];
date2 = ['13-10-1992','14-10-1992'];
Then
find(date == date2)
will return the indices at which the string values are the same
  1 件のコメント
Inês Mendes
Inês Mendes 2015 年 7 月 8 日
and if i have arrays with different sizes?

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2015 年 7 月 8 日
The intersect function works for me, as long as I start with cell arrays as you have stated. E.g.,
>> date1 = {'12-10-1992';'13-10-1992';'14-10-1992';'15-10-1992'};
>> date2 = {'13-10-1992';'14-10-1992'};
>> [d i1 i2] = intersect(date1,date2)
d =
'13-10-1992'
'14-10-1992'
i1 =
2
3
i2 =
1
2
Can you verify again that you really have cell arrays?
  2 件のコメント
Inês Mendes
Inês Mendes 2015 年 7 月 8 日
my problem is that i have found examples of dates that are common to both arrays and they didn´t show in the intersect vector d (in your case)
James Tursa
James Tursa 2015 年 7 月 8 日
Examine those exact mismatches in detail. Are they in exactly the same format? Do either of them have leading or trailing blanks? Etc. Once you know the cause of the mismatch and the formats you are working with, you can design code to deal with it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by