if array A = 10 x 1 with numbers in it and array B = 4 x 1 with numbers in it. How would you create an array C which equals the size of A and is filled with NaN except for the points where A and B equal the same value.
A = [ 1 2 3 4 5 6 7 8 9 10]
B = [ 1 3 5 7]
I would like:
C = [1 NaN 3 NaN 5 NaN 7 NaN NaN NaN]
thanks for the help

 採用された回答

Geoff
Geoff 2012 年 3 月 27 日

1 投票

You can use a real handy function called ismember() to test elements of A against elements of B. Then it's just a matter of indexing:
in = ismember(A,B);
C = nan(size(A));
C(in) = A(in);

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 27 日

1 投票

C = A
C(~ismember(A,B)) = nan
OR
C = A
C(setdiff(A,B)) = nan

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2012 年 3 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by