Find the value which is repeated in each row of a matrix (without loop for)

2 ビュー (過去 30 日間)
Sim
Sim 2022 年 12 月 9 日
コメント済み: Sim 2022 年 12 月 14 日
In each row of the following matrix, one of the values is repeated. For example, in the first row, the value 523 is repeated twice. I would like to find all those values that, in each row, are repeated, but without using a loop for as in the following example. Is it possible ?
% Input
a = [ 523 2920 523 1227
8003 8343 5611 8343
2066 5333 5333 5783
1447 2331 2331 8810
375 8083 8083 8343
5611 6866 5611 8343
2935 7026 5446 7026
1409 6842 6614 6842
2118 7208 4446 7208
4055 4439 4439 4921
42 8656 6691 8656
725 8478 822 8478
1003 1227 1227 6349
921 6614 6614 6842]
a = 14×4
523 2920 523 1227 8003 8343 5611 8343 2066 5333 5333 5783 1447 2331 2331 8810 375 8083 8083 8343 5611 6866 5611 8343 2935 7026 5446 7026 1409 6842 6614 6842 2118 7208 4446 7208 4055 4439 4439 4921
% Would it be possible to perform the same calculation without the loop for ?
for i = 1 : size(a,1)
[v, w] = unique( a(i,:), 'stable' );
duplicate_indices = setdiff( 1:numel(a(i,:)), w );
b(i) = a(i,duplicate_indices);
end
% Output
b'
ans = 14×1
523 8343 5333 2331 8083 5611 7026 6842 7208 4439

採用された回答

Voss
Voss 2022 年 12 月 9 日
Assuming that there's exactly one repeat (i.e., one number appears two times) in each row, which the for-loop solution also assumes, here's one way to do it without a loop:
a = [ 523 2920 523 1227
8003 8343 5611 8343
2066 5333 5333 5783
1447 2331 2331 8810
375 8083 8083 8343
5611 6866 5611 8343
2935 7026 5446 7026
1409 6842 6614 6842
2118 7208 4446 7208
4055 4439 4439 4921
42 8656 6691 8656
725 8478 822 8478
1003 1227 1227 6349
921 6614 6614 6842]
a = 14×4
523 2920 523 1227 8003 8343 5611 8343 2066 5333 5333 5783 1447 2331 2331 8810 375 8083 8083 8343 5611 6866 5611 8343 2935 7026 5446 7026 1409 6842 6614 6842 2118 7208 4446 7208 4055 4439 4439 4921
[m,n] = size(a);
[a_sorted,idx] = sort(a,2);
is_repeat = diff(a_sorted,1,2) == 0;
idx = idx.';
c = idx([is_repeat.'; false(1,m)]);
b = a(sub2ind([m n],(1:m).',c))
b = 14×1
523 8343 5333 2331 8083 5611 7026 6842 7208 4439
  2 件のコメント
Sim
Sim 2022 年 12 月 12 日
編集済み: Sim 2022 年 12 月 12 日
Thank you very much @Voss! :-)
Both solutions are great and I do not know which one to accept! Possible to accept both ?
Voss
Voss 2022 年 12 月 12 日
You're welcome! You can accept at most one answer, but you can vote for as many as you like.

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

その他の回答 (1 件)

Jon
Jon 2022 年 12 月 9 日
編集済み: Jon 2022 年 12 月 9 日
Here's another way
% Input
A = [ 523 2920 523 1227
8003 8343 5611 8343
2066 5333 5333 5783
1447 2331 2331 8810
375 8083 8083 8343
5611 6866 5611 8343
2935 7026 5446 7026
1409 6842 6614 6842
2118 7208 4446 7208
4055 4439 4439 4921
42 8656 6691 8656
725 8478 822 8478
1003 1227 1227 6349
921 6614 6614 6842];
%[C,ia,ic] = unique(A)
Asrt = sort(A,2);
delta = [diff(Asrt,1,2) ones(size(A,1),1)];
[r,c] = find((delta==0)');
B = Asrt';
dup = B(sub2ind(size(B),r,c))
dup = 14×1
523 8343 5333 2331 8083 5611 7026 6842 7208 4439
  4 件のコメント
Jon
Jon 2022 年 12 月 12 日
Your welcome. Unfortunately you can only accept one answer.
Sim
Sim 2022 年 12 月 14 日
Thanks a lot!
I would say it is a pity that there is not the possibility to accept multiple answers :-) :-)

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by