Finding Unique values for First Row of a Matrix

3 ビュー (過去 30 日間)
MShia
MShia 2019 年 12 月 24 日
編集済み: MShia 2019 年 12 月 24 日
I have a matrix which I intend to use, such that, I have to keep the uniqe elements of the first row in a specific manner, Following is to describe the process in a bit detail:
We scan the first row and keep it as such if all the elements are unique. Otherwise for a repeating value we select the next value which is uique. For example for the following matrix A:
CodeCogsEqn.gif
The output should be Output = [5 6 7 9 11], where first element A(1,1) is kept as such, since the the next element in the first row i.e. A(1,2) = A(1,1), we choose next unique element in this column i.e. A(2,2) , And So on to scan the entire first row without repetition. This way to achieve the desired output for generalized matrix ... Any input will be appreciated .
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 12 月 24 日
ismember() the current column against the list of values already used. The first item not present is the one to output and add to the list of values used.
MShia
MShia 2019 年 12 月 24 日
Thanks alot ..

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 12 月 24 日
Let A - your array.
output = A(1,:);
for i = 2:numel(output)
u = unique([output(1:i-1)';A(:,i)]);
output(i) = u(i);
end
  1 件のコメント
MShia
MShia 2019 年 12 月 24 日
編集済み: MShia 2019 年 12 月 24 日
Thanks Sir very elegant solution! The only modification I had to do, was to use the unique command with "stable" option to maintain the sequence. I really appreciate it .

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by