How generate an error to prevent an infinite loop in this function?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 15 Giu 2013
What is A? Please give code to create a sample "A" and call selector(), so we can help you.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements 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!