From: Vivian Harvey on
Hi

This might be a very basic programming query, maybe even not specific to MATLAB. I have two of them.

1> How to make a specialized selective loop processing without making the program too complicated. For example

R=[ A matrix with 'numberofiteration' number of elements ]
J=[];
for m=1: numberofiteration
c=45;
P= somefunction(R(m),c );
J=[J P];
end

The thing is that P could be 0 sometimes and if thats the case then I want c=49 and then I want P recalculated with this different constant so that P=somefunction(R(m),c) ~=0..but I realize there might be some 'jump back to' statements which I am not quite sure how to realize in MATLAB

2> A sort of a switch-case program parallel in MATLAB...

For a matrix which is generated from a loop like the above and has to be within a specific range of the previous non-zero value- so if the previous value is 0, then we use the one before it and if that one is 0, then the one even before that and so on . i cant quite give an example for this one...

Thanks in advance
From: Matt Fig on
For the first question, use a WHILE loop. I don't quite understand the second question.
From: Vivian Harvey on
I have found the solution for the query number 2 , I was trying to find the last non-zero value before the iteration ...

It is simply , for anyone who wants this kind of help, for a matrix A, its max(find(A)) that finds the largest index of the non zero values of A. And we can go from there.

while loop wont work as a 'goto' as it will not jump to a previous line for the same iteration so question 1 is still pending...

Thanks
From: Walter Roberson on
Vivian Harvey wrote:
> I have found the solution for the query number 2 , I was trying to find
> the last non-zero value before the iteration ...
>
> It is simply , for anyone who wants this kind of help, for a matrix A,
> its max(find(A)) that finds the largest index of the non zero values of
> A. And we can go from there.

find(A, 1, 'last') is more efficient than max(find(A))

> while loop wont work as a 'goto' as it will not jump to a previous line
> for the same iteration so question 1 is still pending...

Just recalculate. Oh yes, and for efficiency, pre-allocate:

J = zeros(numberofiteration, 1);
for m = 1: numberofiteration
P = somefunction(R(m), 45);
if P == 0
P = somefunction(R(m), 49);
end
J(m) = P;
end


If the reason you are not currently using a structure like this is that
somefunction is complicated code, then you should probably put the code into a
function (whether or not in the same file) so that you can make the loop as
simple as the above.
From: Vivian Harvey on
I had actually considered doing what you said but it is a bit complicated for that.


I sort of have managed to make an explanation query below and Thanks for your patience in understanding any bit of it.

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.

For example for j=7, P=0 for cons=C(1) as well as cons=C(2) and after recalculating X and P in the same iteration j=7 but with the values of 'cons' changing until say with cons=C(5), P doesnt have a zero value 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). Its like volume adjusting for different frequencies.

I hope this makes sense. I am quite at the end of my tether with just how to incorporate the 'volume adjustment' thing in the program.

Thanks for any suggestions- I have no idea what I could call this query..