check for a pair in power hand

3 views (last 30 days)
Jason Earls
Jason Earls on 23 Mar 2021
Edited: Jan on 23 Mar 2021
i am trying to check for a pair, I need to compare each value in an array to each other in the array.
the difference will be a multuple of 13. exampe 1 and 14 would be a pair.
if mod (card[ct1]-card[ct2],13) ==0; but not sure how to write it. any help appreciated.

Accepted Answer

Rik
Rik on 23 Mar 2021
The feasibility of this depends on the size of the arrays involved. If your vectors are too large, the implicit expansion will cause problems.
v1=randi(26,8,1);
v2=randi(26,8,1);
[ind1,ind2]=find(mod(v1-v2.',13)==0);
pairs=[v1(ind1),v2(ind2)]
pairs = 3×2
8 8 21 8 13 26

More Answers (1)

Jan
Jan on 23 Mar 2021
Edited: Jan on 23 Mar 2021
The trivial approach would be two nested loops:
card = randi(20, 1, 1000);
result = zeros(2, numel(card)); % Pre-allocate to maximum size
c = 0;
for i1 = 1:numel(card)
for i2 = 1:numel(card)
if mod(card(i1) - card(i2), 13) == 0
c = c + 1;
result(1, c) = i1;
result(2, c) = i2;
end
end
end
result = result(:, 1:c); % Crop unused elements
Rik's vectorized apporach is smarter.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!