From: Walter Roberson on
Vivian Harvey wrote:

> A very simplified template of my program is as below...
>
> A=[a set of n elements ];
> C=[a set of elements];
> R=[];
> cons=C(1);
> for j=1:n
>
> while P~=0
> X=somefunction(A(j),cons);
> P= anotherfunction(X);
> **<Here is the insert where
> I want 'X' recalculated with a different value of 'cons'>
>
> end
> R= [R; P];
> end

> ** What I am trying to do is that have X recalculated with different
> value of 'cons' such that P does not have a zero value. And the values
> of 'cons' can only be taken from C matrix.


> when the program can go into the next iteration j=8. What I want is
> that in the iteration j=8, X is calculated with cons=C(5) and not
> started over again with cons=C(1)

You did not indicate what should happen if you reach the end of C without
having found a 0. I have gone ahead and incorporated two protection mechanisms
for this: when the end of C is reached, it goes back to the beginning of C;
and it keeps track to ensure that you do not keep cycling through C infinitely
if there are no values of C that make P zero. This latter check also happens
to provide "useful work" for the while loop; I recall that you did not like
"while true" because it felt like a worthless loop condition (or something
like that.).

considx = 1;
for j = 1:n
cons_started_from = considx;
tried_all_c = false;
while ~tried_all_c
X = somefunction(A(j), C(considx));
P = anotherfunction(X);
if P ~= 0; break; end
considx = 1 + mod(considx, length(C));
tried_all_c = considx == cons_started_from;
end
if ~tried_all_c
R = [R; P];
else
error('OhNo', sprintf('Did not find any non-zero P for entry %d, value
%g', j, A(j));
end
end
From: Vivian Harvey on
Thanks for replying so speedily ...I am trying to get my head around the code and hopefully will finally get a positive end to this...
From: Vivian Harvey on
Hi

Just wanted to feedback that it works and so a big Thank You :)

Regards Viv