Setting non-repeated elements from two related matrices to NaN

1 回表示 (過去 30 日間)
For the matrices A and B,
A = [1 2 1; 3 5 1; 2 3 6]
B = [1 2 3; 4 5 6; 7 8 9]
set the non-repeated elements of A to NaN, setting also to NaN the related elements of B. By non-repeated elements I refer to elements that only occur once through the whole matrix. Generate the two new matrices from those:
A_nan = [1 2 1; 3 NaN 1; 2 3 NaN]
B_nan = [1 2 3; 4 NaN 6; 7 8 NaN]
Thank so much in advance, just trying to manipulate the matrix before a Regression between A and B.

採用された回答

the cyclist
the cyclist 2015 年 9 月 12 日
A = [1 2 1; 3 5 1; 2 3 6];
B = [1 2 3; 4 5 6; 7 8 9];
uniqueA = unique(A);
counts = histcounts(A,[uniqueA;Inf]);
nanIndex = ismember(A,uniqueA(counts==1));
A(nanIndex) = NaN;
B(nanIndex) = NaN;
  1 件のコメント
Victor Francisco Pajuelo Madrigal
Victor Francisco Pajuelo Madrigal 2015 年 9 月 12 日
Thank you very much, this is precisely what I was looking for! Clear and beautiful code, helps me to speed up the learning process. Thanks!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by