Matrices won't take values and output from function.

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.

 Accepted Answer

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 Comments

I'm not really sure what your loops are showing. My keep variable is a counter, so I don't think it needs to be involved in a loop of its own.
for i=1:4
x1 = X(i);
y1 =Y(i);
if (x1^2 + y1^2 <= 4)
keep = keep +1;
end
end
This section adds up the number of vertices contained in the circle and it isn't until I have the answer that I go into the next section, where I am using the if statements to determine whether or not I break up the square or add it to finX and finY. So, I'm not sure how any of those loops would help because I don't need to determine what to do until I have the number.
Believe me, I've been experimenting :P but when no matter what I do, or what input I give it, nothing will go into finX, finY even when I see that "keep = 4"... I'm at a loss.
The other possibility I mentioned, that you define:
function[finX,finY] = recurse(A,B,finX,finY)
but that this line (at the end of your if keep ... loop):
recurse(finalX, finalY, finX, finY);
doesn’t return any output arguments could be your problem. I didn’t run your code, so see if changing it to:
[finX,finY] = recurse(finalX, finalY, finX, finY);
improves things.
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.
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.

Sign in to comment.

More Answers (1)

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! :)

Categories

Community Treasure Hunt

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

Start Hunting!