how to replace elements of a matrix in a sequence by comparing with another?

2 ビュー (過去 30 日間)
suchismita
suchismita 2014 年 6 月 30 日
編集済み: Image Analyst 2014 年 6 月 30 日
i have two matrix as
y=[12 14 13 18 17 19 16 20 13 19 18];
z=[1 2 3 4 5 6 8 9 10 12 14];
Now whenever there is a repetition in y, I want that value replaced by the values of z.
In the 9th position is '13' which is a repetition of 3rd position so that will get replaced by z's first value, which is '1', Then in 10th position is again a repetition so this '19' will get replaced by '2'. Same will happen whenever there is a repetition.
Please please help me out...

採用された回答

Image Analyst
Image Analyst 2014 年 6 月 30 日
It's not fancy, but did you try taking the histogram (to find integers with counts of 2 or greater) and then just the normal, brute force for loop to replace them with the numbers you want?
  2 件のコメント
suchismita
suchismita 2014 年 6 月 30 日
actually i dont want any repetition in y if repeating than can i change with any number which is not there in y...like that way also can i do???? that also will work for me
suchismita
suchismita 2014 年 6 月 30 日
how can i replace the repetition values by anything else...?????

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 30 日
y = y(:);
z = z(:);
[~,b] = unique(y,'first');
c = setdiff(z,y);
ii = setdiff((1:numel(y))',b);
out = zeros(size(y));
jj = sort(b);
out(jj) = y(jj);
out(ii) = c(1:numel(ii));

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by