Borrar filtros
Borrar filtros

Matrices won't take values and output from function.

1 visualización (últimos 30 días)
Christyn Phillippi
Christyn Phillippi el 28 de Mzo. de 2014
Editada: Star Strider el 28 de Mzo. de 2014
if true
function[finX,finY] = recurse(A,B,finX,finY)
for j=1:4
temp1 = A(j,:);
temp2 = B(j,:);
split(temp1, temp2, finX, finY);
end
function split(X,Y, finX, finY)
wid = abs((X(4)- X(1))/2);
if wid >= (1/8);
finalX = zeros(4,4);
finalY = zeros(4,4);
keep = 0;
for i=1:4
x1 = X(i);
y1 =Y(i);
if (x1^2 + y1^2 <= 4)
keep = keep +1;
end
end
if keep == 1 || 2 || 3
finalX(1,1) = X(1);
finalY(1,1) = Y(1);
finalX(1,2) = X(1);
finalY(1,2) = Y(1)+wid;
finalX(1,3) = X(1)+wid;
finalY(1,3) = Y(1)+wid;
finalX(1,4) = X(1)+wid;
finalY(1,4) = Y(1);
finalX(2,1) = X(1);
finalY(2,1) = Y(1)+wid;
finalX(2,2) = X(1);
finalY(2,2) = Y(1)+2*wid;
finalX(2,3) = X(1)+wid;
finalY(2,3) = Y(1)+2*wid;
finalX(2,4) = X(1)+wid;
finalY(2,4) = X(1)+wid;
finalX(3,1) = X(1)+wid;
finalY(3,1) = Y(1)+wid;
finalX(3,2) = X(1)+wid;
finalY(3,2) = Y(1)+2*wid;
finalX(3,3) = X(1)+2*wid;
finalY(3,3) = Y(1)+2*wid;
finalX(3,4) = X(1)+2*wid;
finalY(3,4) = Y(1)+wid;
finalX(4,1) = X(1)+wid;
finalY(4,1) = Y(1);
finalX(4,2) = X(1)+wid;
finalY(4,2) = Y(1)+wid;
finalX(4,3) = X(1)+2*wid;
finalY(4,3) = Y(1)+wid;
finalX(4,4) = X(1)+2*wid;
finalY(4,4) = Y(1);
recurse(finalX, finalY, finX, finY);
elseif keep == 4
finX = [finX;X]; %won't change
finY = [finY;Y]; %won't change
else
end
end
end
end
end
I'm building a square mesh over a circle of radius 2. Giving the function two 4x4 matrices which contains the X and Y coordinates of the 4 vertexes of 4 squares, I remove the first row (first square) from each matrix and then in "split" I test each point to see if it is contained in the circle. If 1-3 points are contained, I split that square into 4 smaller ones and call recurse on those matrices. If 4 points are contained, I'm trying to add the rows I pulled out (so that square) into finX and finY. In the end, It is supposed to return finX and finY with each row being the coordinates of a square that will belong in the mesh.
I cannot for the life of me figure out why none of the values will go into finX or finY when they keep (my vertex counter) is equal to 4. It just.. skips it.
I tried to "Run and Advance" with breakpoints but I get the error :
function [finX,finY] = recurse1(A,B,finX,finY) | Error: Function definitions are not permitted in this context.
Not really sure why. I think because I have variables and I'm supposed to run it in the main console, but I don't know how to check breakpoints when I run it there.

Respuesta aceptada

Star Strider
Star Strider el 28 de Mzo. de 2014
Editada: Star Strider el 28 de Mzo. de 2014
You need to structure your if blocks differently.
To illustrate:
disp('Loop #1')
for keep = 1:4
if keep == 1 || 2 || 3
q1 = keep
elseif keep == 4
Q2 = keep
end
end
disp('Loop #2')
for keep = 1:4
if keep == 1 | 2 | 3
q1 = keep
elseif keep == 4
Q2 = keep
end
end
disp('Loop #3')
for keep = 1:4
if keep == 1
q1 = keep
elseif keep == 2
q2 = keep
elseif keep == 3
q3 = keep
elseif keep == 4
Q2 = keep
end
end
disp('Loop #4')
for keep = 1:4
if keep < 4
q1 = keep
elseif keep == 4
Q2 = keep
end
end
The Q2 variable is only defined in Loop #3 and Loop #4 in this example. The structure in Loop #4 may be most appropriate for your application.
There may be other problems. I didn’t run your code to look. That in this line:
recurse(finalX, finalY, finX, finY);
the call to recurse doesn’t return any results could be a problem.
When in doubt, experiment!
  4 comentarios
Christyn Phillippi
Christyn Phillippi el 28 de Mzo. de 2014
Sorry, my eyes must have jumped over that line. Unfortunately it didn't fix it D:.
Still returning an empty matrix. Going to do some searching regarding debugging in MATLAB and maybe I can find some success when I'm able to go over it bit by bit.
Star Strider
Star Strider el 28 de Mzo. de 2014
Editada: Star Strider el 28 de Mzo. de 2014
The ‘Loop #4’ idea didn’t work either? The idea there is that your loop is never getting to:
finX = [finX;X]; %won't change
finY = [finY;Y]; %won't change
To test that idea, right after:
elseif keep == 4
put:
disp('Keep == 4')
If that displays, the loop structure isn’t the problem.
I can’t run your code, so I’m out of ideas.

Iniciar sesión para comentar.

Más respuestas (1)

Christyn Phillippi
Christyn Phillippi el 28 de Mzo. de 2014
I figured it out!
Apparently every time the it called split:
for j=1:4
temp1 = A(j,:);
temp2 = B(j,:);
split(temp1, temp2, finX, finY);
end
it was resetting the matrix, and this was solved by changing it to:
for j=1:4
temp1 = A(j,:);
temp2 = B(j,:);
[finX,finY] = split(temp1, temp2, finX, finY);
end
So, your suggestion, just on another function! Thank you for your help! :)
  1 comentario
Star Strider
Star Strider el 28 de Mzo. de 2014
Editada: Star Strider el 28 de Mzo. de 2014
My pleasure (albeit serendipitously)!

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by