Mapping elements of a matrix using a vector array

13 ビュー (過去 30 日間)
AP
AP 2014 年 5 月 21 日
コメント済み: Javier 2017 年 5 月 24 日
I have a matrix named A with size N×3. The values of the elements of A ranges from 1 to M. For example, A with size 8×3:
A = [ 1 2 3;
1 4 5;
5 6 7;
1 3 4;
5 6 7;
1 4 7;
4 3 2;
7 8 9]
B is a vector array whose elements are the values of element of A. In the example above, B with size 9×1:
B = [1 2 3 4 5 6 7 8 9]
I have another vector array named C with the size of B with which I want to replace element of B in A. In fact, C represents whether the elements of B in A has repeated more than for example 2. In my example, C with size 9×1:
C = [0 0 0 0 0 0 0 1 1]
Here is the expected result, where the elements 1 to 7 are replaced by 0 and 8 and 9 are replaced by 1:
Anew = [ 0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 1 1]
How can I replace the elements of B in A with its corresponding values using C? Thank you.
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 21 日
What is the expected result?

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

採用された回答

the cyclist
the cyclist 2014 年 5 月 21 日
編集済み: the cyclist 2014 年 5 月 21 日
I am not 100% sure of which of the following two possibilities you want, given your example, but I think either
Anew = ismember(A,B(logical(C)))
or
[~,idx] = ismember(A,B);
Anew = C(idx)
is what you want. I think it's the latter.
  1 件のコメント
Javier
Javier 2017 年 5 月 24 日
The second option matches the expected. Great! Thank you, cyclist

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by