check for a pair in power hand
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Rik
il 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)]
0 Commenti
Più risposte (1)
Jan
il 23 Mar 2021
Modificato: Jan
il 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Operators and Elementary Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!