How generate an error to prevent an infinite loop in this function?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'd like to know if it's possible to generate an error in this code(function) to prevent an infinite loop... I put an Initial matrix A of dimension 4x4. I want to make 8 groups by using this function (explanations below code):
function [B, idx1, idx2] = selector(A)
m=8; %number of groups
n=numel(A);
ii=0;
idx=[];
while ii<m
Permutation=randperm(n);
id=setdiff(Permutation,idx,'stable');
[ ii1,jj1]=ind2sub(size(A),id(1));
[ ii2,jj2]=ind2sub(size(A),id(2));
if or(abs(ii1-ii2)==1 & abs(jj1-jj2)==0,abs(ii1-ii2)==0 & abs(jj1-jj2)==1);
ii=ii+1;
idx=[idx id(1:2)];
B(ii,:)=[A(id(1)) A(id(2))];
end
end
disp(idx);
[idx1,idx2]=ind2sub(size(A),idx);
end
This function just returns groups (2 by 2) from elements of the matrix A. (these elements are adjacent (not diagonally) in A. And they're taken only one time) The matrix B contains these groups. (Just run the function to see) The function just run into an infinite loop when the last elements chosen in A are not adjacent and there are no other possibilities to group them. Thank You very much.
2 个评论
Image Analyst
2013-6-15
What is A? Please give code to create a sample "A" and call selector(), so we can help you.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!