check for a pair in power hand
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Rik
2021-3-23
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 个评论
更多回答(1 个)
Jan
2021-3-23
编辑:Jan
2021-3-23
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!