remove non-unique columns even if the sign has changed

19 ビュー (過去 30 日間)
LC
LC 2025 年 2 月 4 日 14:13
コメント済み: Les Beckham 2025 年 2 月 4 日 15:12
I have a 76x16 matrix. Some of the data is repeated and I want to remove repeated columns, which I have tried to do below. However, the numbers are the same but the sign has changed, e.g. columns 8 and 14 are the same data but the signs are reversed (where 8 is +ve, 14 is -ve etc), which my code doesn't acocunt for. The +ve/-ve matters so I cant just abs() the dataset.
I want to keep the first occurence of the repeated columns, where I should be left with 6 columns of data. Can anybody help?
mlag= unique(newca1.','rows','stable').'
mlag(:,max(mlag) == 0) = [];

採用された回答

Les Beckham
Les Beckham 2025 年 2 月 4 日 14:46
Perhaps this?
load newca.mat
[~, icol, ~] = unique(abs(newca1).', 'rows', 'stable')
icol = 7×1
1 2 3 4 7 8 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mlag = newca1(:,icol)
mlag = 76×7
0 0 0 0 0 0.1250 0 0 0 0 0 0 0.1111 0 0 0 0 0 0 0.1000 0 0 0.0909 0.0909 0.0909 0.0909 0.0909 0 0 0.1667 0.1667 0.1667 0.0833 0.0833 0 0 0.1538 0.1538 0.2308 0.0769 0.0769 0 0 0.3571 0.0714 0.3571 -0.2143 0.1429 0.2857 0 0.1333 0.0667 0.3333 0 0.3333 0.2667 0 0.1333 0.0667 0.3333 -0.0667 0.3333 0.2667 0 0.1333 0.0667 0.2667 -0.0667 0.3333 0.2667
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
LC
LC 2025 年 2 月 4 日 14:58
Thank you!
Les Beckham
Les Beckham 2025 年 2 月 4 日 15:12
You are quite welcome.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2025 年 2 月 4 日 14:49
編集済み: Steven Lord 2025 年 2 月 4 日 14:49
Make a copy of newca1, let's call it M. Multiply each column of M whose first element is negative by -1. This way if you think of your columns as vectors, they're all pointing "right" (towards +Inf along the x axis, if this were a 2-dimensional problem.) Call unique on M and ask not only for the unique rows but also the indices of the unique rows. Use the indices to extract the corresponding columns of newca1.

カテゴリ

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