How generate an error to prevent an infinite loop in this function?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 15 de Jun. de 2013
What is A? Please give code to create a sample "A" and call selector(), so we can help you.
Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!