フィルターのクリア

Identifying same values of two arrays

2 ビュー (過去 30 日間)
Mia Dier
Mia Dier 2021 年 6 月 26 日
回答済み: Cris LaPierre 2021 年 6 月 26 日
I have two string arrays: array1 and array2. I want to create a variable named change that is equal to 0 if array1 and array2 are equal or one of them is NaN and 1 otherwise.
array1 array2 change
A A 0
A A 0
A C 1
C C 0
C C 0
NaN B 0
  3 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 26 日
Further to @Soniya Jain's comment, what are A, B, and C in array1 and array2? Are they variables? Or, are they character or string literals? If they are variables, what type of data do they hold?
Mia Dier
Mia Dier 2021 年 6 月 26 日
Dear Soniya,
I edited my question and fixed that part. I couldn't come up any solution other than assigning unique numbers to each value, taking difference of the arrays and creating variable change that is equal to 1 if the difference is not 0. But I'm trying to find an easier way.
Dear Scott,
array1 and array2 are string arrays. So, I believe that they store string literals.
Thank you for your comments. I hope the question makes more sense now.

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

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 6 月 26 日
Note that nan means not a number. When the rest of your data is chars or strings, nan actually gets convereted to <missing>.
array1 = ["A" "A" "A" "C" "C" nan]';
array2 = ["A" "A" "C" "C" "C" "B"]';
array1(ismissing(array1)) = array2(ismissing(array1));
array2(ismissing(array2)) = array1(ismissing(array2));
change = array1~=array2
change = 6×1 logical array
0 0 1 0 0 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by