Let me describe a task: I have 3 matrices (M1,M2,M3), they each have lenght(Mi) rows and 2 columns. We are given a function g(x,s) where s is a 2 dimensional parameter and x and eta are given. I want to check first matrix M1, if there exists an s such as g(x,M1(i,:)>eta I want to END the alghoritm and set s_new=M1(i,:). If such s inside M1 does not exist I want to go to matrix M2 and search inside it. Next matrix M3. If such s_new does not exist inside all of the matrices, I want to break.
My first try:
function[s_new]= checking(M1,M2,M3,x)
bool1=0;
eta = 10^-8;
g = @(x,s) x-s(1)-s(2);
while bool1==0
for i=1:length(M1)
if g(x,M1(i,:))>eta
s_new=M1(i,:);
bool1=1;
end
end
for i=1:length(M2)
if g(x,M2(i,:))>eta
s_new=M2(i,:);
bool1=1;
end
end
for i=1:length(M3)
if g(x,M3(i,:))>eta
s_new=M3(i,:);
bool1=1;
end
end
bool1=1;
end
My second try involved some break option but it didn't work either. The problem is: alghoritm does not stop when it finds s in M1 such as our condition holds, it goes to M2 and if it finds there such s, it changes s_new. Also in order to save some time I dont want alghoritm to go through the matrix M2 if such s exsists in M1.
Example why it works badly:
M1=[0,-1;0,-1], M2=[0,-2;0,-2], M3=[0,0;0,0], x=0
It should return vector [0,-1] and returns [0,-2] instead. Any help appreciated.
EDIT: the bool1=1 inside the for loops are underlined with red, which states bool1 might be unused as if it didnt recognise it from the condition at start while bool1=0