get common position in cell array and replace with specific values

1 回表示 (過去 30 日間)
Elysi Cochin
Elysi Cochin 2019 年 3 月 20 日
編集済み: Jan 2019 年 3 月 21 日
idx = [ 3 6 7 ] ;
ids = [ 2 3 5 6 7 ] ;
i have a cell of size 1 x 8
Initially all values in cell_array of size 1 x 8 is 'AB'
i want, the value in cell_array positions [3 6 7] (which is common in both idx and ids) to be wriiten as 'A' and the other values not in idx as 'B'
and those positions not in idx or ids, leave as it is (in our case 'AB')

採用された回答

Jan
Jan 2019 年 3 月 20 日
編集済み: Jan 2019 年 3 月 21 日
C = repmat({'AB'}, 1, 8);
idx = [ 3 6 7 ] ;
ids = [ 2 3 5 6 7 ] ;
index = intersect(ids, idx);
C(index) = {'A'};
index = setdiff(ids, idx);
C(index) = {'B'};
Or leaner:
m = ismember(ids, idx);
C(ids(m)) = {'A'};
C(ids(~m)) = {'B'};
Or:
Pool = {'B', 'A'};
C(ids) = Pool(ismember(ids, idx) + 1)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by