Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
loop stuck in the second round but not in first
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to write a code that will create two arrays of squares with the same total area but different number of boxes.
There is a loop that creates the matrix M which state the ratio, the number of the squers at the left and the number of the squers at the right.
Then, for every row in M, it create matrix G1 and matrix G2 with the proper amount of square. The code stops when I am trying to create a figure of the matrix "Main" (combination of G1 and G2), in the first round the loop works fine, and it stops at the second round. (the code is attached)
the code:
function [Main]= MatrixOfRatios1 ()
% that function eccepts range of number of squares, ratios and total area per marix, and
% create a matrix with the ratio, small number and large number of squares.
Ra=[0.2 0.5];%range of ratios
minsq=1;%the minimal number of squares (by user)
maxsq=9;%the maximal number of squares (by user)
Nr=minsq:1:maxsq;%the range of the sqare number
L=1;
for i=1:length (Nr); % for all the range of number of squares
for j=1:length (Ra);%for all the range of ratios
M(L,1:3)= [Ra(j), Nr(i), Nr(i)/Ra(j)]; % M is the matrix that includes the ratio between the number of squares, the number of squares of the right and the left matrix
L=L+1;
end
end
F=M(:,3)<maxsq;% index all the raws where the 3rd column (number of squers on the left) is smaller than maxsq
M=M(F,:);% new matrix compose only with the numbers of squares that are in range :)
A=25; %A is the total area of each matrix (by user)
sos=200/A; %sos is the size of the small matrix (before we extend it so the squares will be spaced)
sos=round(sos);%rounding the size of the matrix
for i=1:size(M,1) %loop to create the random matrix (can also be a functio of its own)
[G1,G2] = createFigure(A,i,M,sos);
Main1 = CombineMatrix(G1,G2);
imshow(Main1);
pause
R1 = num2str(M(i,1));
R2 = num2str(M(i,2));
R3 = num2str(M(i,3));
i = num2str(i);
MainFile = strcat('Main_',R1,'_',R2,'_',R3,'_',i,'.jpg');
saveas(gca,MainFile)
Main2 = CombineMatrix(G2,G1);
MainFile = strcat('Main_',R1,'_',R3,'_',R2,'_',i,'.jpg');
saveas(gca,MainFile)
imshow(Main2);
pause
clear('Main')
end
end
function [G1,G2] = createFigure(A,i,M,sos)
NumOfSqR =M(i,2); %setting the number of square (smaller number-right side
SmallR=ones(sos); %setting the size of the small matrix(right side) before the extension, to avoid squares overlap
R = randperm(numel(SmallR));%randomizing zeros inside a matrix of ones -step 1
SmallR(R(1:NumOfSqR)) = 0; %randomizing zeros inside a matrix of ones- step 2
[y x]=find(SmallR==0); %using 2 indexes (x and y) to locate the zeros (to enlarge them later)
a=sqrt(A/(M(i,2)));
g=ones(200);
for j=1:length(x)
g (round(A*(y(j))-a):round(A*y(j)), round(A*(x(j))-a):round(A*x(j)))=0;
end
G1(1:200,1:200,i)=g;%create fig1
NumOfSqR =M(i,3); %setting the number of square (smaller number-right side
SmallR=ones(sos); %setting the size of the small matrix(right side) before the extension, to avoid squares overlap
R = randperm(numel(SmallR));%randomizing zeros inside a matrix of ones -step 1
SmallR(R(1:NumOfSqR)) = 0; %randomizing zeros inside a matrix of ones- step 2
[y x]=find(SmallR==0); %using 2 indexes (x and y) to locate the zeros (to enlarge them later)
a=sqrt(A/(M(i,2)));
g=ones(200);
for j=1:length(x)
g (round(A*(y(j))-a):round(A*y(j)), round(A*(x(j))-a):round(A*x(j)))=0;
end
G2(1:200,1:200,i)=g;
end
function Main = CombineMatrix (G1, G2)
Main=ones(200,500);
Main(1:200, 1:200)=G1;
Main(1:200, 301:500)=G2;
end
3 commentaires
Andrew Newell
le 8 Fév 2011
Also, please read <http://www.mathworks.com/matlabcentral/about/answers/ |About MATLAB Answers|>: "Include as much code as you need, but as little as you can get away with."
Matt Fig
le 8 Fév 2011
Yes, prune this down to the offending lines. Include a small representation of the variables used in those lines.
Réponses (0)
Cette question est clôturée.
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!