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!